Search code examples
pythoneval

Why is eval('pass') a SyntaxError?


I was trying to temporarily null out a string I was passing to eval to do nothing (I didn't remove it because preserving the order was important for my hacky profile script), and was somewhat miffed that when I gave it 'pass' it dumped on me, likewise with an empty string or some equivalent do-nothing statement.

>>> eval('pass')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    pass
       ^
SyntaxError: unexpected EOF while parsing

I eventually just fed it None, but why was it a syntax error? "Unexpected EOF" perplexes me; the string is a complete statement in-and-of-itself. Does eval not tolerate keywords?


Solution

  • Quoting from eval's documentation:

    The expression argument is parsed and evaluated as a Python expression.

    Then, pass is a statement, not an expression.