multiple namespaces within a single module?
ethan at stoneleaf.us
Thu Feb 9 19:05:41 EST 2012
Ethan Furman wrote:
> Hrm -- and functions/classes/etc would have to refer to each other that
> way as well inside the namespace... not sure I'm in love with that...
Not sure I hate it, either. ;)
Slightly more sophisticated code:
def __init__(self, current_globals):
self.globals = current_globals
self.saved_globals = current_globals.copy()
def __exit__(self, *args):
new_items = 
for key, value in self.globals.items():
if (key not in self.saved_globals and value is not self
or key in self.saved_globals
and value != self.saved_globals[key]):
for key, value in new_items:
setattr(self, key, value)
if __name__ == '__main__':
x = 'inside main!'
with NameSpace(globals()) as a:
x = 'inside a?'
with NameSpace(globals()) as b:
x = 'inside b?'
y = 'still inside main'
More information about the Python-list