Creating a local variable with a parameterized name

Nic Williams nic at csee.uq.edu.au
Sun Jan 30 22:22:25 EST 2000


In article <vndaelomfpu.fsf at betelgeuse.ccs.neu.edu>,
  Justin Sheehy <dworkin at ccs.neu.edu> wrote:
> Nic Williams <nic at csee.uq.edu.au> writes:
>
[more cutting]
> > def f(name):
> > 	locals()[name]=123
> > 	print locals()[name]
>
> This is why I didn't recommend that approach, from the Python
documentation:
>
> locals ()
>        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.
>
> Yes, I know that it works right now, but it is also quite clearly
> marked as something which could stop working without warning in a
> future version of Python.
>
> If you must recommend something like that, it is only polite to point
> out that it is strictly incorrect code and should not be relied upon.

It is clearly marked having read it carefully, and I will now politely
point out that it is incorrect code :-) I do believe that globals() is
ok though to use and modify, though it does clearly appear that vars()
and locals() should not be modified.

Sorry for missing that after merely running some test code to test my
theory.

nic


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list