这篇文章主要介绍了Python中的exec、eval使用实例,本文以简洁的方式总结了Python中的exec、eval作用,并给出实例,需要的朋友可以参考下
通过exec可以执行动态Python代码,类似Javascript的eval功能;而Python中的eval函数可以计算Python表达式,并返回结果(exec不返回结果,print(eval(“…”))打印None);
代码如下:
>>> exec(“print(\”hello, w来源gao@daima#com搞(%代@#码网orld\”)”)
hello, world
>>> a = 1
>>> exec(“a = 2”)
>>> a
2
以上就是Python中的exec、eval使用实例的详细内容,更多请关注gaodaima搞代码网其它相关文章!