An expression that rebinds a variable?
Maric Michaud
maric at aristote.info
Thu May 17 12:15:06 EDT 2007
GreenH a écrit :
> Can I know what kind of expressions rebind variables, of course unlike
> in C, assignments are not expressions (for a good reason)
> So, eval(expr) should bring about a change in either my global or
> local namespace, where 'expr' is the expression
>
For global scope you could use globals().__setitem__('x', 5) but it's
not possible in local scope because the dict returned by locals() in
function is not where the local variables are really stored.
So the preferred way is to use :
In [39]: exec "x=5"
which the same as :
In [40]: eval(compile('x=5', '<string>', 'exec'))
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
More information about the Python-list
mailing list