usuallythe local scope references the local names of the (textuallycurrent function outside functionsthe local scope references the same namespace as the global scopethe module' namespace class definitions place yet another namespace in the local scope it is important to realize that scopes are determined textuallythe global scope of function defined in module is that module' namespaceno matter from where or by what alias the function is called on the other handthe actual search for names is done dynamicallyat run time -howeverthe language definition is evolving towards static name resolutionat "compiletimeso don' rely on dynamic name resolution(in factlocal variables are already determined statically special quirk of python is that if no global 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 deletionsthe statement del removes the binding of from the namespace referenced by the local scope in factall operations that introduce new names use the local scopein particularimport statements and function definitions bind the module or function name in the local scope the global statement can be used to indicate that particular variables live in the global scope and should be rebound therethe nonlocal statement indicates that particular variables live in an enclosing scope and should be rebound there scopes and namespaces example this is an example demonstrating how to reference the different scopes and namespacesand how global and nonlocal affect variable bindingdef scope_test()def do_local()spam "local spamdef do_nonlocal()nonlocal spam spam "nonlocal spamdef do_global()global spam spam "global spamspam "test spamdo_local(print("after local assignment:"spamdo_nonlocal(print("after nonlocal assignment:"spamdo_global(print("after global assignment:"spamscope_test(print("in global scope:"spamthe output of the example code isafter local assignmenttest spam after nonlocal assignmentnonlocal spam after global assignmentnonlocal spam in global scopeglobal spam note how the local assignment (which is defaultdidn' change scope_test' binding of spam the nonlocal assignment changed scope_test' binding of spamand the global assignment changed the module-level binding python scopes and namespaces