简体   繁体   中英

Python using exec with custom globals

I would like to create a simple 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 ? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM