1

Code:

eval("print('foobar')")

Output:

Traceback (most recent call last):
  File "Untitled.py", line 30, in <module>
    eval("print('foobar')")
  File "<string>", line 1
    print('foobar')
        ^
SyntaxError: invalid syntax

What am I doing wrong?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • 1
    What your python version? – gzc May 28 '17 at 10:52
  • Given that it throws a `SyntaxError` it's python-2.x. You need `exec` for statements like `print` in python 2. – MSeifert May 28 '17 at 10:57
  • w.r.t. the duplicate: I'm voting to leave this open. The potential duplicate does not mention `print` specifically, so in terms of internet search queries leaving the current question open would help. – kfx May 28 '17 at 12:02

3 Answers3

2

print is an operator in Python 2, and a function in Python 3. Which version do you use?

  • ^ This is essentially the correct answer. `eval` works on expressions only. In Python 3, `print` is a function, and so its use is a valid expression. In Python 2, its use is a statement, not an expression, so it won't evaluate. See https://stackoverflow.com/questions/22558548/eval-syntaxerror-invalid-syntax-in-python for more info – daveruinseverything May 28 '17 at 10:55
  • If you remove the "which version do you use" part and say that's the reason why it fails on python-2.x it would be an even better answer. – MSeifert May 28 '17 at 10:59
  • Using Python 2 so I want exec() then? Makes a lot of sense, thanks – Ibraheem Rodrigues May 28 '17 at 11:00
  • @IbraheemRodrigues You don't want to use `exec` or `eval` - trust me! But if you have to use them, then you need `exec` for `print` in python 2. – MSeifert May 28 '17 at 11:01
  • 2
    I would use "statement" instead of "operator". https://docs.python.org/2/reference/simple_stmts.html#print – mzjn May 28 '17 at 11:02
  • @MSeifert In terms of security (code injection)? I am using it to make a [brainfuck](https://en.wikipedia.org/wiki/Brainfuck) interpreter. – Ibraheem Rodrigues May 28 '17 at 15:13
2

You have to use the exec function. Like this:

exec("print('foobar')")

See What is the difference between an expression and a statement in Python? for more informations.

Zcode
  • 425
  • 3
  • 7
2

eval evaluates expressions, not statements, so you need to pass it the print function, not the print statement. By default, print is a statement in Python 2, and the print statement doesn't exist in Python 3. However, the print function is available in recent versions of Python 2 via a __future__ import. The print function is actually defined in those versions of Python 2 but it's masked by the print statement; the import makes the print statement unavailable, thus exposing the print function.

Demo, tested on Python 2.6.6:

from __future__ import print_function

eval("print('foobar')")    

output

foobar

BTW, it's generally not a good idea to use eval or exec, unless you have no alternative. They are relatively slow, and have security risks if you pass them unsanitized strings to evaluate / execute. For details, please see Eval really is dangerous by SO veteran Ned Batchelder. To evaluate simple Python literals you can use ast.literal_eval.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182