JadedTuna
JadedTuna

Reputation: 1823

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

Upvotes: 2

Views: 166

Answers (1)

falsetru
falsetru

Reputation: 368944

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

Upvotes: 3

Related Questions