Creating variables from dicts
bruno.42.desthuilliers at websiteburo.invalid
Wed Feb 24 12:48:13 CET 2010
Luis M. González a écrit :
> Alright, this is what the docs say about locals:
> The built-in functions globals() and locals() return the current
> global and local dictionary, respectively, which may be useful to pass
> around for use as the second and third argument to exec().
> The default locals act as described for function locals() below:
> modifications to the default locals dictionary should not be
> attempted. Pass an explicit locals dictionary if you need to see
> effects of the code on locals after function exec() returns."
> I wonder why updating locals(), not from within a function, works (at
> least in my interactive session).
Because at the top level, locals and globals are the same thing.
> And what about the trick of updating globals? Is it legal?
It's legal, but it's (usually) a very bad idea - at the top-level, it
harms readability, and from within a function it's doubly bad
(readibility + "globals are evil").
Now as usual with GoldenRules(tm), it's meant to be broken - once you do
know why you shouldn't _usually_ do it.
for the very same reasons global
> If not, is
> there any "legal" way to do what the OP needs?
More information about the Python-list