|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
The scope of a name is the range of statements in the text of a program in which that name can be used to refer to an object. Python defines three scopes--local , global , and built-in . In Python scopes are called namespaces . When a name is used to refer to an object in a Python program the namespaces are searched in the following order to find the binding for that name: local namespace first, then global namespace, then built-in namespace. This is the so-called LGB rule .
When a Python function is called, a new local namespace is created. By default, name bindings created inside a function are created in the local namespace of that function. Name bindings created at the top-level of a module (or file) are created in the global namespace. The built-in namespace contains the bindings for the pre-defined names of Python.