setting variables in the local namespace
Dave Angel
davea at ieee.org
Tue Oct 13 14:44:03 EDT 2009
Mick Krippendorf wrote:
> <snip>
>
> Yes, and, uh, yes. "locals()['foo'] = bar" works in that it does the
> same thing as "foo = bar". So why don't you write that instead?
>
> Mick.
>
>
I wouldn't expect it to do the same thing at all, and it doesn't, at
least not in Python 2.6.2. It may store the "bar" somewhere, but not in
anything resembling a local variable.
bar = 42
def mytestfunc():
stuff = 9
locals()['stuff'] = bar
print locals()['stuff']
print stuff
mytestfunc()
prints 9, twice. No sign of the 42 value.
DaveA
More information about the Python-list
mailing list