eval:计算字符串中的表达式
exec:执行字符串中的语句
execfile:用来执行一个文件

在python 2中exec是语句,在python3中exec变为函数,后面要跟括号。在python3中取消了execfile语句,可以通过

with open('test1.py','r') as f:

    exec(f.read())

语句实现execfile语句。

eval可以直接将字符串转成list tuple 字典。但是如果字符串内的句子为执行语句,如“__import__('os').system('dir >dir.txt')”,会存在运行系统命令的危险。所以用eval时候需要谨慎。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2023-03-16
相关资源
相似解决方案