Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20121
| Date | 2012-02-09 16:05 -0800 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: multiple namespaces within a single module? |
| References | <6f50fa3d-74e6-4682-b5d8-2634d418dd23@d15g2000yqg.googlegroups.com> <jh1725$slv$1@dough.gmane.org> <4F3436CA.2020800@stoneleaf.us> <jh1f8a$td4$1@dough.gmane.org> <4F3447EA.6070601@stoneleaf.us> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5621.1328831986.27778.python-list@python.org> (permalink) |
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:
<code>
class NameSpace(object):
def __init__(self, current_globals):
self.globals = current_globals
self.saved_globals = current_globals.copy()
def __enter__(self):
return self
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]):
new_items.append((key, value))
for key, value in new_items:
setattr(self, key, value)
del self.globals[key]
self.globals.update(self.saved_globals)
if __name__ == '__main__':
x = 'inside main!'
with NameSpace(globals()) as a:
x = 'inside a?'
def fn1():
print(a.x)
with NameSpace(globals()) as b:
x = 'inside b?'
def fn1():
print(b.x)
def fn2():
print('hello!')
b.fn1()
y = 'still inside main'
a.fn1()
b.fn1()
print(x)
print(y)
</code>
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
multiple namespaces within a single module? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-02-09 10:32 -0800
Re: multiple namespaces within a single module? Peter Otten <__peter__@web.de> - 2012-02-09 20:33 +0100
Re: multiple namespaces within a single module? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-02-09 15:24 -0800
Re: multiple namespaces within a single module? Peter Otten <__peter__@web.de> - 2012-02-10 12:10 +0100
Re: multiple namespaces within a single module? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-02-10 08:28 -0800
Re: multiple namespaces within a single module? Ethan Furman <ethan@stoneleaf.us> - 2012-02-09 13:12 -0800
Re: multiple namespaces within a single module? Peter Otten <__peter__@web.de> - 2012-02-09 22:53 +0100
Re: multiple namespaces within a single module? Ethan Furman <ethan@stoneleaf.us> - 2012-02-09 14:25 -0800
Re: multiple namespaces within a single module? Ethan Furman <ethan@stoneleaf.us> - 2012-02-09 16:05 -0800
Re: multiple namespaces within a single module? Arnaud Delobelle <arnodel@gmail.com> - 2012-02-10 10:41 +0000
csiph-web