User defined lexical scoping... can I do this?
Thomas Jollans
t at jollybox.de
Tue Sep 18 17:51:13 EDT 2012
On 09/18/2012 10:50 PM, weissman.mark at gmail.com wrote:
> Well there's wired stuff like this:
>
> In [1]: locals()["x"] = 5
>
> In [2]: print x
> 5
>
No, there isn't. Modifying the dictionary returned by locals() has no
effect.
>>> def f ():
... locals()["x"] = 1
... return x
...
>>> f ()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
NameError: global name 'x' is not defined
>>>
>>> locals()["x"] = 1
>>> x
1
>>> #this works because
... locals() is globals()
True
>>>
The exception is the case when local scope is identical to global scope.
In this case, locals() has globals() semantics.
More information about the Python-list
mailing list