4.2.2 Resolution of names The Python Language Reference, Release 3.9.5 A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s environment . When a name is not found at all, a NameError exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an $_{UnboundLocalError}$exception is raised.$_{UnboundLocalError}$ is a subclass of$_{NameError}$ . If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the$_{global}$ statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module$_{builtins}$. The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The$_{global}$ statement must precede all uses of the name. The$_{global}$ statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The$_{nonlocal}$ statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope.$_{SyntaxError}$is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called$_{__main__}$ . Class definition blocks and arguments to$_{exec()}$ and$_{eval()}$ are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: CPython implementation detail: Users should not touch$_{__builtins__}$ ; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should$_{import}$ the$_{builtins}$module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name __builtins__ in its global namespace; this should be a dictionary or a module (in the latter case the module’s dic- tionary is used). By default, when in the$_{__main__}$ module,$_{__builtins__}$ is the built-in module$_{builtins}$; when in any other module,$_{__builtins__}$ is an alias for the dictionary of the$_{builtins}$module itself. Chapter 4. Execution model 46 classA : a =42 b =list(a+i for i in range(10 )) 4.2.3 Builtins and restricted execution