Question about name scope

Ian Kelly ian.g.kelly at gmail.com
Wed Feb 1 18:47:53 EST 2012


On Wed, Feb 1, 2012 at 4:41 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> I'm not sure what you mean by temporary:
>
> --> def f(x, y):
>
> ...     frob = None
> ...     loc = locals()
> ...     loc[x] = y
> ...     print(loc)
> ...     print(locals())
> ...     print(loc)
> ...     print(locals())
> ...
> -->
> --> f('frob', 19)
> {'y': 19, 'x': 'frob', 'frob': 19}
> {'y': 19, 'x': 'frob', 'frob': None, 'loc': {...}}
> {'y': 19, 'x': 'frob', 'frob': None, 'loc': {...}}
> {'y': 19, 'x': 'frob', 'frob': None, 'loc': {...}}
>
> Seems to be stuck that way.

The first print is the one that is incorrect.  It suggests that the
local 'frob' has been changed to 19 as it has in the dict, but the
actual value of the local is still None.  The second print on
accurately reflect that.



More information about the Python-list mailing list