You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def f(x):
def g():
x = 'abc'
print('x =', x)
def h():
z = x
print('z =', z)
x = x + 1
print('x =', x)
h()
g()
print('x =', x)
return g
Book says If the name is found (which it is in this case), the value to which it is bound (4) is used. If it is not found there, an error message is produced.
I'm not sure if above is correct. My understanding of LEGB is python will look in the closest enclosing scope, then global. x does not have to be defined in the immediately enclosing scope of f, but any enclosing scope, searching outwards through all enclosing functions until it finds one. Failing which, it goes to global scope to find x, which will happen in this example if x is not found in scope of def f, and print the global x=3, so no error message is produced.
The text was updated successfully, but these errors were encountered:
Book says
If the name is found (which it is in this case), the value to which it is bound (4) is used. If it is not found there, an error message is produced
.I'm not sure if above is correct. My understanding of LEGB is python will look in the closest enclosing scope, then global. x does not have to be defined in the immediately enclosing scope of f, but any enclosing scope, searching outwards through all enclosing functions until it finds one. Failing which, it goes to global scope to find x, which will happen in this example if x is not found in scope of
def f
, and print the global x=3, so no error message is produced.The text was updated successfully, but these errors were encountered: