python -i (interactive environment)

Steven Bethard steven.bethard at gmail.com
Tue Mar 8 11:37:18 EST 2005


Joe wrote:
> Isn't this a bug?
> 
> Here's the test program:
> 
> import code
> 
> def test_func():
>     lv = 1
>     print '\n\nBEFORE lv: %s\n' % (lv)
>     code.interact(local=locals())
>     print '\n\nAFTER  lv: %s\n' % (lv)
>     return

Check the documentation for locals() [1]:

"Update and return a dictionary representing the current local symbol 
table. Warning: The contents of this dictionary should not be modified; 
changes may not affect the values of local variables used by the 
interpreter."

So if you change things in the dictionary returned by locals() you won't 
actually change the local variables.

STeVe

[1] http://docs.python.org/lib/built-in-funcs.html#l2h-45



More information about the Python-list mailing list