Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想创建一个简单的python shell。所以现在我被困在执行代码上。问题是,有没有办法exec使用自定义全局变量运行?谢谢
exec
您可以按如下方式传递自定义全局字典:
a, b = 1, 2 exec('print(a+b)', {'a': 5, 'b': 5}) # prints 10
参考:exec声明