var change local var def glob ()global var var + declare global (normalchange global var def glob ()var import thismod thismod var + change local var import myself change global var def glob ()var import sys glob sys modules['thismod'glob var + change local var import system table get module object (or use __name__change global var def test()print(varlocal()glob ()glob ()glob (print(varwhen runthis adds to the global variable (only the first function does not impact it)import thismod thismod test( thismod var this worksand it illustrates the equivalence of globals to module attributesbut it' much more work than using the global statement to make your intentions explicit as we've seenglobal allows us to change names in module outside function it has close relative named nonlocal that can be used to change names in enclosing functionstoo--but to understand how that can be usefulwe first need to explore enclosing functions in general scopes and nested functions so fari've omitted one part of python' scope rules on purposebecause it' relatively uncommon to encounter it in practice howeverit' time to take deeper look at the letter in the legb lookup rule the layer was added in python it takes the form of the local scopes of any and all enclosing function' local scopes enclosing scopes are sometimes also called statically nested scopes reallythe nesting is lexical one-nested scopes correspond to physically and syntactically nested code structures in your program' source code text scopes and nested functions