simple eval question

Gerhard Häring gh_pythonlist at gmx.de
Fri Nov 2 14:59:01 EST 2001


On Fri, Nov 02, 2001 at 12:41:28PM -0800, N Becker wrote:
> What is wrong with this?
> 
> eval ("print \"hello\"')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<string>", line 1
>     print "hello"
>         ^
> SyntaxError: invalid syntax
> 
> I expected that eval takes a string an evaluates it as input to the
> interpreter, as if it had been typed into the command line.  Was I
> wrong?

You were almost right. eval() takes an expression, while exec() takes
statements. print is a statement, not an expression, so you need exec()
here.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list