this is my code :
a = \
'''def fun():\n
print 'bbb'
'''
eval(a)
fun()
but it shows error :
Traceback (most recent call last):
File "c.py", line 8, in
eval(a)
File "", line 1
def fun():
^
SyntaxError: invalid syntax
so what can i do ,
thanks
解决方案
eval() with string a string argument is only for expressions. If you want to execute statements, use exec:
exec """def fun():
print 'bbb'
"""
But before you do that, think about whether you really need dynamic code or not. By far most things can be done without.