Namespaces are just Python objects, and you can assign objects (including the result of attribute lookups) to local variable names:
strands = translation.strands
active = translation.active
locus = translation.locus
Alternatively, you'd have to hack together a context manager that modifies locals(), as shown in this answer.
Something like this would do that:
import inspect
class Namespace(object):
def __init__(self, namespaced):
self.namespaced = namespaced
def __enter__(self):
"""store the pre-contextmanager scope"""
ns = globals()
namespaced = self.namespaced.__dict__
# keep track of what we add and what we replace
self.scope_added = namespaced.keys()
self.scope_before = {k: v for k, v in ns.items() if k in self.scope_added}
globals().update(namespaced)
return self
def __exit__(self):
ns = globals()
# remove what we added, then reinstate what we replaced
for name in self.scope_added:
if name in ns:
del ns[name]
ns.update(self.scope_before)
then use it like this:
with Namespace(translation):
del strands[active][locus]
where all items in the translation.__dict__ are made available globally while in the while block.
Note that this is not thread-safe, and has the potential to create lots of confusion for anyone trying to read code that uses this in future, including yourself. Personally, I wouldn't use this.