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
globalornonlocalstatement 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 xremoves the binding ofxfrom the namespace referenced by the local scope. In fact, all operations that introduce new names use the local scope: in particular,importstatements and function definitions bind the module or function name in the local scope.
But you should avoid code like this anyway.