Python scopes and namespaces
Python Tutorial - scopes and namespaces
This stuff is possible:
|
|
# Footguns
|
|
global_foo
here is not being assigned. Instead, a local variable is created and assigned. See also global
and nonlocal
statements.
A special quirk of Python is that – if no
global
ornonlocal
statement is in effect – assignments to names always go into the innermost scope. Assignments do not copy data — they just bind names to objects. The same is true for deletions: the statementdel x
removes the binding ofx
from the namespace referenced by the local scope. In fact, all operations that introduce new names use the local scope: in particular,import
statements and function definitions bind the module or function name in the local scope.
But you should avoid code like this anyway.