eval() 将字符串类型的代码执行并返回结果

print(eval('1+2+3+4'))

exec()将自字符串类型的代码执行

print(exec("1+2+3+4"))
exec("print('hello,world')")
code = '''
import os 
print(os.path.abspath('.'))
'''
code = '''
print(123)
a = 20
print(a)
'''
a = 10
exec(code,{'print':print},)
print(a)
指定global参数

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2021-06-19
  • 2021-07-19
猜你喜欢
  • 2022-12-23
  • 2021-12-16
  • 2021-09-06
  • 2021-10-18
  • 2021-11-09
  • 2021-10-23
  • 2022-02-26
相关资源
相似解决方案