this orderin the local scopein any enclosing functionslocal scopesin the global scopeand finally in the built-in scope the first occurrence wins the place in your code where variable is assigned usually determines its scope in python xnonlocal declarations can also force names to be mapped to enclosing function scopeswhether assigned or not also keep in mind that these rules apply only to simple variable names ( spamin parts and viwe'll see that qualified attribute names ( object spamlive in particular objects and follow completely different set of lookup rules than those covered here references to attribute names following periods search one or more objectsnot scopesand in fact may invoke something called inheritance in python' oop modelmore on this in part vi of this book other python scopespreview though obscure at this point in the bookthere are technically three more scopes in python--temporary loop variables in some comprehensionsexception reference variables in some try handlersand local scopes in class statements the first two of these are special cases that rarely impact real codeand the third falls under the legb umbrella rule most statement blocks and other constructs do not localize the names used within themwith the following version-specific exceptions (whose variables are not available tobut also will not clash withsurrounding codeand which involve topics covered in full later) the scope lookup rule was called the "lgb rulein the first edition of this book the enclosing def "elayer was added later in python to obviate the task of passing in enclosing scope names explicitly with default arguments-- topic usually of marginal interest to python beginners that we'll defer until later in this since this scope is now addressed by the nonlocal statement in python xthe lookup rule might be better named "lngbtodaybut backward compatibility matters in bookstoo the present form of this acronym also does not account for the newer obscure scopes of some comprehensions and exception handlersbut acronyms longer than four letters tend to defeat their purposepython scope basics