chaeny 2019. 7. 16. 05:06

Nonlocal

def g(x) :

  def f():

    x = 10

    x = x + 2

  f()

  print(x)

g(20)

 

def g(x):

  def f():

    x = x - 8

  f()

  print(x)

g(20)

 

By default,

-   you can access variables in parent frames

-   you cannot modify variables in parent frames

nonlocal statements allow you to modify a name in a parent frame instead of creating a new binding in the current frame