Namespace and Objects
Namespace
is a mapping from names to objects including built-in names (e.g. abs()), global names in a module and the local names in a function.
- You can access to any names in a module by
modname.attributeName
. - Any attribute in the module is writable. e.g. modname.the_answer = 42.
- You can also delete the attribute from the object. e.g. del modname.the_answer
- The built-in names is created when Python interpreter starts and never deleted.
- The global namesapce of a module is created when the module definition is read in, the module namespace last until the interpreter quits.
- The local namespace of a function is created when the function is called and deleted when the function returns or raises an unhandled exception
Scope
is a textual region of a Python program where a namespace is directly accessible.
- ‘nonlocal’ statement can be used to rebind a variable found outside.
- ‘global’ statement can be used to indicate that paticular variables live in the global scpe and rebould here