statements and expressions

Cliff Wells logiplexsoftware at earthlink.net
Wed Jan 9 23:50:03 CET 2002


On 9 Jan 2002 23:36:58 +0100
Chris Liechti <cliechti at gmx.net> wrote:

> "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' ")

[snip]

> 
> in your case both fail because you use exec instead of eval...

What do you mean "both fail"?  The first one works and is in fact the
basically the same as your own example:

exec "print 'hello'"
exec("print 'Carlos' ") 

What's the difference?  The parentheses are superfluous, but harmless.

Your information is correct though.

> >>> something = "hello"
> >>> x = eval("something")
> >>> x
> 'hello'
> >>> exec "print 'hello'"
> 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.
> 

-- 
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308




More information about the Python-list mailing list