understand what' going on incidentallyknowledge about this subject is useful for any advanced python programmer let' begin with some definitions namespace is mapping from names to objects most namespaces are currently implemented as python dictionariesbut that' normally not noticeable in any way (except for performance)and it may change in the future examples of namespaces arethe set of built-in names (containing functions such as abs()and built-in exception names)the global names in moduleand the local names in function invocation in sense the set of attributes of an object also form namespace the important thing to know about namespaces is that there is absolutely no relation between names in different namespacesfor instancetwo different modules may both define function maximize without confusion -users of the modules must prefix it with the module name by the wayi use the word attribute for any name following dot -for examplein the expression realreal is an attribute of the object strictly speakingreferences to names in modules are attribute referencesin the expression modname funcnamemodname is module object and funcname is an attribute of it in this case there happens to be straightforward mapping between the module' attributes and the global names defined in the modulethey share the same namespace! attributes may be read-only or writable in the latter caseassignment to attributes is possible module attributes are writableyou can write modname the_answer writable attributes may also be deleted with the del statement for exampledel modname the_answer will remove the attribute the_answer from the object named by modname namespaces are created at different moments and have different lifetimes the namespace containing the built-in names is created when the python interpreter starts upand is never deleted the global namespace for module is created when the module definition is read innormallymodule namespaces also last until the interpreter quits the statements executed by the top-level invocation of the interpretereither read from script file or interactivelyare considered part of module called __main__so they have their own global namespace (the built-in names actually also live in modulethis is called builtins the local namespace for function is created when the function is calledand deleted when the function returns or raises an exception that is not handled within the function (actuallyforgetting would be better way to describe what actually happens of courserecursive invocations each have their own local namespace scope is textual region of python program where namespace is directly accessible "directly accessiblehere means that an unqualified reference to name attempts to find the name in the namespace although scopes are determined staticallythey are used dynamically at any time during executionthere are at least three nested scopes whose namespaces are directly accessiblethe innermost scopewhich is searched firstcontains the local names the scopes of any enclosing functionswhich are searched starting with the nearest enclosing scopecontains non-localbut also non-global names the next-to-last scope contains the current module' global names the outermost scope (searched lastis the namespace containing built-in names if name is declared globalthen all references and assignments go directly to the middle scope containing the module' global names to rebind variables found outside of the innermost scopethe nonlocal statement can be usedif not declared nonlocalthose variables are read-only (an attempt to write to such variable will simply create new local variable in the innermost scopeleaving the identically named outer variable unchanged except for one thing module objects have secret read-only attribute called __dict__ which returns the dictionary used to implement the module' namespacethe name __dict__ is an attribute but not global name obviouslyusing this violates the abstraction of namespace implementationand should be restricted to things like post-mortem debuggers classes