Re: An expression that rebinds a variable?
GreenH [EMAIL PROTECTED] writes: 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! -- http://mail.python.org/mailman/listinfo/python-list
An expression that rebinds a variable?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: An expression that rebinds a variable?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: An expression that rebinds a variable?
On May 17, 9:15 am, Maric Michaud [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: An expression that rebinds a variable?
En Thu, 17 May 2007 18:29:35 -0300, GreenH [EMAIL PROTECTED] 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. 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. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list