implicit variable declaration and access

Tom Anderson twic at urchin.earth.li
Wed Jun 15 06:52:12 EDT 2005


On Tue, 14 Jun 2005, Scott David Daniels wrote:

> Tom Anderson wrote:
>> ... If it's not, try:
>> x = "myVarName"
>> y = "myVarValue"
>> locals()[x] = y
>
> Sorry, this works with globals(), but not with locals().

Oh, weird. It works when i tried it.

Aaaah, i only tried it at the interactive prompt. If i actually try 
writing a function which does, that, yes, i get:

>>> def foo():
...     locals()["myvar"] = 42
...     print myvar
...
>>> foo()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 3, in foo
NameError: global name 'myvar' is not defined
>>>

My bad.

tom

-- 
Gens una summus.



More information about the Python-list mailing list