User defined lexical scoping... can I do this?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Sep 19 00:03:02 EDT 2012
On Tue, 18 Sep 2012 21:38:19 -0400, Terry Reedy wrote:
> On 9/18/2012 5:51 PM, Thomas Jollans wrote:
>> 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.
>
> Last time I tried it, it does within a class -- in cpython at least.
> That locals dict usually becomes the __dict__ of the class. But not to
> be depended on indefinitely and across implmentations.
Exactly. The behaviour of modifying the dict returned by locals() is not
defined. For example, this is what happens under Python 2.6, Jython 2.5,
and IronPython 2.6:
steve at runes:~$ cat test.py
a = b = 'global'
def test():
a = None
locals()['a'] = 'local'
locals()['b'] = 'local'
print a, b
test()
steve at runes:~$ python test.py
None global
steve at runes:~$ jython test.py
None global
steve at runes:~$ ipy test.py
local global
Other Python implementations may do differently.
--
Steven
More information about the Python-list
mailing list