Difference between eval and exec ??

Raymond Hettinger python at rcn.com
Tue Mar 26 04:18:56 EST 2002


"Thomas Weholt" <thomas at gatsoft.no> wrote in message
news:uEWn8.8$Vme.170401280 at news.telia.no...
> Eval and exec seem to have the same effect. What's the real difference?

One is for expressions and the other for statements.
Eval computes the value of an expression like, 3*x+1.
Exec is for commands like import this or print that.

Exec itself is a statement that compiles and runs code strings
as if you had typed them in a .py file and ran the file.

Eval is an expression that can be embedded in other
expressions or used on the right hand side of an assignment:

>>> x = eval('3+4') * 10
>>> print x
70


>
> In this case : docs != enlightenment.

Hmm, this raises the question of whether my
   answer == enlightenment
or
   answer is enlightenment
or
   answer in ['obfuscation', 'muddledthought', 'misinformation']


>
> Best regards,
> Thomas Weholt
>
>
>

Raymond Hettinger





More information about the Python-list mailing list