An expression that rebinds a variable?

G

GreenH

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
 
M

Maric Michaud

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
 
G

GreenH

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
 
G

Gabriel Genellina

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'.

As already said, it's hard to make changes to the local namespace, but the
global namespace is directly accessible.

py> z = {'a': 1}
py> eval("z.setdefault('b',2)")
2
py> z
{'a': 1, 'b': 2}

eval is unsafe by definition, even if you provide your own namespaces. If
you can't trust the expression to be evaluated, don't use eval if you are
minimally concerned about security.
 
T

Thomas Bellman

GreenH said:
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

List comprehensions:
Traceback (most recent call last):
File said:
>>> eval('[ord(c) for c in "parrot"]') [112, 97, 114, 114, 111, 116]
>>> c
't'

This is supposed to be changed in Python 3.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top