yet another modifying locals() question
Ed Anuff
ed.anuff at gmail.com
Sun Sep 13 21:52:47 EDT 2009
I know that locals() is not supposed to be modifiable under most
circumstances, but I'm trying to solve a situation where I'm
dynamically generating some class attributes and it seemed to be the
best way, so I tried something out that seems to work but I'm not sure
that it's kosher:
>>> def f(l):
... l['b'] = 1
...
>>> class A:
... f(locals())
...
>>> A.b
1
In my code, I'm doing something quite a bit more complex than just
assigning a single attribute, but this is the simplest use case
example.
Is there a reason why this works and is it safe to rely on it or is
there a better approach? BTW, this works in a program too, it's not
just an artifact of the command line interpreter globals() = locals()
thing.
Thanks
Ed
More information about the Python-list
mailing list