0
def function():
    exec('a=2')
    print(vars())
    print(a)
function()

a=1
def function():
    global a
    exec('a=2')
    print(vars())
function()
print(a)

Above: The output was {'a': 2}, but then the error message said name 'a' is not defined.
Below: I still got {'a': 2}, the a outside was still 1.

Well, what's the difference between a = 2 and exec('a = 2')?

SkyLian
  • 1
  • 1

0 Answers0