How do I execute a string containing Python code in Python?

后端 未结 14 2405
一向
一向 2020-11-22 02:42

How do I execute a string containing Python code in Python?

相关标签:
14条回答
  • 2020-11-22 03:09

    eval() is just for expressions, while eval('x+1') works, eval('x=1') won't work for example. In that case, it's better to use exec, or even better: try to find a better solution :)

    0 讨论(0)
  • 2020-11-22 03:13

    Check out eval:

    x = 1
    print eval('x+1')
    ->2
    
    0 讨论(0)
提交回复
热议问题