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.m...@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@runes:~$ cat test.py 

a = b = 'global'
def test():
    a = None
    locals()['a'] = 'local'
    locals()['b'] = 'local'
    print a, b

test()

steve@runes:~$ python test.py 
None global
steve@runes:~$ jython test.py 
None global
steve@runes:~$ ipy test.py 
local global


Other Python implementations may do differently.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to