An expression that rebinds a variable?

GreenH Green.Horn.000 at gmail.com
Thu May 17 17:29:35 EDT 2007


On May 17, 9:15 am, Maric Michaud <m... at aristote.info> wrote:
> 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

Thanks, But, my interest is actually in finding the cases in which
eval(expr) would throw surprises at me by bringing changes in
namespace(s), just because I haven't given a namespace for that eval()
i.e., where would we see the perils of not passing namespace to the
'eval'.

-Green




More information about the Python-list mailing list