2

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

JadedTuna
  • 1,783
  • 2
  • 18
  • 32

1 Answers1

3

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

falsetru
  • 357,413
  • 63
  • 732
  • 636