Scope of the global statement
From Python Reference
The global operator has the same scope as the name bind operation in the same block.
If the closest enclosing scope for a free variable contains a global operator, the free variable is considered global.
What do the two sentences mean?
Can you also provide examples to explain what they mean?
Thank.
source to share
The global operator has the same scope as the name bind operation in the same block.
It says where the instruction applies global
.
Basically, under normal conditions, when you do:
foo = 1
inside a function, it makes the foo
variable local to that function; even if the name is only assigned at the end of the function, it is local from the beginning, it does not switch from global to local at the destination.
Likewise, if your function includes the line:
global foo
it makes it foo
global to the entire function, even if it global foo
is the last line in the function.
The important part is that it doesn't matter where in the function you do it. As well as:
def x():
print(y)
y = 1
calls UnboundLocalError
(because assignment y
makes it local to the entire scope of the function and you print it before giving it a value) by doing:
y = 0
def x():
print(y)
y = 1
global y
will print the global value y
(0 for the first call, 1 on the second) on the first line without errors (instead of hoisting UnboundLocalError
or whatever), because the instructions global
always apply for the whole function, both before and after where they actually appear, just as local variables are local to the entire scope of a function, even if only assigned at the end. Note that modern Python raises the value SyntaxWarning
to use the global name before the associated operator global
, so it's best to put expressions global
for clarity and avoid warnings.
Part about nested areas:
If the closest enclosing scope for a free variable contains a global operator, the free variable is considered global.
covers the really unusual corner case with multiple nested scopes, where the outer scopes assigns to a local variable, the scopes within which declares a name global
, and the scopes within which are used (but not assigned) a name. Short definition: "If you are looking for a variable to read but not in the local scope as you are looking at the outer scopes for it, if it is global in one of them, stop checking the nested scopes and go straight to the global scope" show by example:
foo = 1
def outermost():
def middle():
global foo # Stops scope checking, skips straight to global
def innermost():
print(foo)
return innermost
foo = 2 # Doesn't change global foo
return middle
With this definition, execution outermost()()()
infers 1
as the scope innermost
scans in validate middle
, determines what foo
is global to the scope, middle
and skips the validation outermost
straight to the global foo
.
If you had:
foo = 1
def outermost():
def middle():
# No global declaration
def innermost():
print(foo)
return innermost
foo = 2 # Doesn't change global foo
return middle
then there will be a way out 2
; searching foo
in innermost
would not find it locally or in the area middle
, but it will find it in the area outermost
and pull it out of there. It is highly unlikely that you will see such a construct, but language documents should be unambiguous whenever possible.
source to share