[Tutor] Re: A simple (newbie) question.

D-Man dsh8290@rit.edu
Thu, 22 Feb 2001 09:30:58 -0500


On Thu, Feb 22, 2001 at 02:13:56PM +0000, mike.mellor@tbe.com wrote:
| D - 
| I tried it, and couldn't make it work this way either.  Here is what 
| I got:
| 
| >>> eval(c)
| Traceback (most recent call last):
|   File "<pyshell#4>", line 1, in ?
|     eval(c)
|   File "<string>", line 1
|     print "Python"
|         ^
| SyntaxError: invalid syntax
| 
| 
| Am I doing something wrong?  Thanks.

Oh, yeah, I forgot -- Python makes a distinction between statements
and expressions.  eval() only works on expressions and returns the
value of the expression.  exec() handles statements, but doesn't
return anything.  (If I had tried what I suggested I would have seen
it not work)  For this example, use  exec( c )  If you want to try
eval, try something like

a = "3"
b = "4" 
c = a + " + " + b
d = eval( c )
print d


(I tested this this time ;-))

-D

PS.  I believe that Common Lisp and Scheme don't make this
distinction since all "statements" are actually functions.  The funny
thing is I haven't used CL or Scheme much so I should remember the
Python way more than the Lisp way.