How to `eval` code with `def`?
Peng Yu
pengyu.ut at gmail.com
Sun May 28 21:03:33 EDT 2017
Hi,
I got the following error when I try to eval the following code with
def. Does anybody know what is the correct way to evaluation python
code that contains `def`? Thanks.
$ cat ./main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
import dis
s = """
def f(x):
return x is not None
"""
print(s)
eval(s)
$ ./main.py
def f(x):
return x is not None
Traceback (most recent call last):
File "./main.py", line 10, in <module>
eval(s)
File "<string>", line 2
def f(x):
^
SyntaxError: invalid syntax
--
Regards,
Peng
More information about the Python-list
mailing list