[英]Python using exec with custom globals
I would like to create a simple python shell. 我想创建一个简单的python shell。 So right now I am stuck on executing the code.
所以现在我被困在执行代码上。 The question is, is there a way to run
exec
with custom globals ? 问题是,有没有一种方法可以使用自定义全局变量运行
exec
? Thanks 谢谢
You can pass custom global dictionary as follow: 您可以按照以下方式传递自定义全局词典:
a, b = 1, 2
exec('print(a+b)', {'a': 5, 'b': 5}) # prints 10
Reference: The exec
statement 参考:
exec
语句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.