I\'m having a bit of trouble understanding what\'s going wrong with the following function:
def ness():
pie=\'yum\'
vars()[pie]=4
print vars()[pie]
print yum
It's not safe to modify the dict returned by vars()
vars([object])¶
Without an argument, act like locals().
With a module, class or class instance object as argument (or anything else that has a dict attribute), return that attribute.
Note
The returned dictionary should not be modified: the effects on the corresponding symbol table are undefined.
Your second example is a special case. vars()
is equivalent to globals()
in the global namespace, and the dict returned by globals()
behaves as you would expect ( but is frowned upon )
>>> id(vars()),id(globals())
(3085426868L, 3085426868L)