statements and expressions
cliechti at gmx.net
Wed Jan 9 23:36:58 CET 2002
"Alves, Carlos Alberto - Coelce" <calves at coelce.com.br> wrote in
news:mailman.1010608272.28800.python-list at python.org:
> Why this two codes doesn't work?!
> 1º case:
> exec("print 'Carlos' ")
> 2º case:
> x = exec("something")
> In former case, is it beacouse exec(..) requires an expression as
> argument and print "...." is a statement? And in the latter, beacouse
> statements doesn't support assignments?!
> Thanks in advance,
in your case both fail because you use exec instead of eval...
do the following examples help?
>>> something = "hello"
>>> x = eval("something")
>>> exec "print 'hello'"
eval is a function and needs an expression.
exec is a statement and executes arbitrary python code but does not return
anything as it is no function.
Chris <cliechti at gmx.net>
More information about the Python-list