I feel like this could have some interesting side uses (typing on mobile so ignore the crappy indentation):
with Namespace('a'): x = 1 y = 2 print(a.__dict__) # we now have a dict The GN build system flat-out has no dictionary / mapping type in favor of scopes like this. On Sun, Jul 28, 2019, 9:36 PM Steven D'Aprano <st...@pearwood.info> wrote: > On Fri, Jul 26, 2019 at 10:52:46AM -0400, Calvin Spealman wrote: > > > Let's say you do this or any of the variants suggested... What does this > do? > > > > a = {"foo": 1} > > b = {} > > with a: > > with b: > > foo = 0 > > Re-writing that to my suggested version: > > with namespace("a") as a: > with namespace("b") as b: > foo = 0 > > will create a namespace object (a subclass of ModuleType) and bind it to > "a", containing a namespace object "b", containing a variable "foo": > > assert isinstance(a, NamespaceType) > assert isinstance(a.b, NamespaceType) > assert a.b.foo == 0 > > > That would be analogous to this existing code: > > class a: > class b: > foo = 0 > > > except classes have different scoping rules to modules. > > > -- > Steven > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/W3XEBOHQS3AWJ3Z26GA3H24GBGECM6FL/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/REXZHAZ2SW3CJMQHB7IETICF3XNVMRS4/ Code of Conduct: http://python.org/psf/codeofconduct/