isa namespace in which variables created (assignedat the top level of the module file live global variables become attributes of module object to the outside world after imports but can also be used as simple variables within the module file itself the global scope spans single file only don' be fooled by the word "globalhere--names at the top level of file are global to code within that single file only there is really no notion of singleall-encompassing global file-based scope in python insteadnames are partitioned into modulesand you must always import module explicitly if you want to be able to use the names its file defines when you hear "globalin pythonthink "module assigned names are local unless declared global or nonlocal by defaultall the names assigned inside function definition are put in the local scope (the namespace associated with the function callif you need to assign name that lives at the top level of the module enclosing the functionyou can do so by declaring it in global statement inside the function if you need to assign name that lives in an enclosing defas of python you can do so by declaring it in nonlocal statement all other names are enclosing function localsglobalsor built-ins names not assigned value in the function definition are assumed to be enclosing scope localsdefined in physically surrounding def statementglobals that live in the enclosing module' namespaceor built-ins in the predefined built-ins module python provides each call to function creates new local scope every time you call functionyou create new local scope--that isa namespace in which the names created inside that function will usually live you can think of each def statement (and lambda expressionas defining new local scopebut the local scope actually corresponds to function call because python allows functions to call themselves to loop--an advanced technique known as recursion and noted briefly in when we explored comparisons--each active call receives its own copy of the function' local variables recursion is useful in functions we write as wellto process structures whose shapes can' be predicted ahead of timewe'll explore it more fully in there are few subtleties worth underscoring here firstkeep in mind that code typed at the interactive command prompt lives in moduletooand follows the normal scope rulesthey are global variablesaccessible to the entire interactive session you'll learn more about modules in the next part of this book also note that any type of assignment within function classifies name as local this includes statementsmodule names in importfunction names in deffunction argument namesand so on if you assign name in any way within defit will become local to that function by default python scope basics