By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

An expression that rebinds a variable?

P: n/a
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

May 17 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
May 17 '07 #2

P: n/a
On May 17, 9:15 am, Maric Michaud <m...@aristote.infowrote:
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

May 17 '07 #3

P: n/a
En Thu, 17 May 2007 18:29:35 -0300, GreenH <Gr************@gmail.com>
escribió:
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.

pyz = {'a': 1}
pyeval("z.setdefault('b',2)")
2
pyz
{'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.

--
Gabriel Genellina

May 17 '07 #4

P: n/a
GreenH <Gr************@gmail.comwrites:
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:
>>c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined
>>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.
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"What sane person could live in this world ! bellman @ lysator.liu.se
and not be crazy?" -- Ursula K LeGuin ! Make Love -- Nicht Wahr!
May 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.