Python . .
global, .
. :
x = 'global'
def f():
x = 'local in f'
def g():
global x
x = 'assigned in g'
g()
print x
f() local in f, x - 'assigned in g'.
Python 3 nonlocal, .
x = 'global'
def f():
x = 'local in f'
def g():
nonlocal x
x = 'assigned in g'
return g
print(x)
f() ', g (which is the value of x in local scope of f() ), while value of x` .
, Python () , x :
x = 'global'
def f():
x = 'local in f'
def g():
nonlocal x
x = 'assigned in g'
return g
g = f()
g()
source
share