Python name binding

Source: Internet
Author: User

Python name binding
In Python, objects are associated and referenced by names. Python introduces a name through the name binding operation. The so-called code block in Python is a program that acts as the execution unit. For example, module, function, and class definition. Commands entered in the interactive environment are also a type of code block. A Python script file is also a code block. In addition, when we use the command specified by the-c option on the command line, it is also a code block. The string parameters passed to the built-in functions eval () and exec () are also a type of code block. A code block is executed in the form of execution frames. An execution frame contains some management information and can be used for debugging. The execution frame also specifies where the current code block will be executed and how the subsequent code will be executed. The scope in Python defines the visibility of the name in the code block. If a local variable is defined in the Code block, the scope of the local variable is the code block. If this definition occurs in the function body, the scope of the variable is extended to any code block contained in the function. However, if it is included in a code block of the function, if the same name is bound to different objects, the external name cannot be extended to this code block. Def out_func (): # The scope of a is in the out_func function. a = 0b = 0def in_func (): # The scope of a is extended from out_func to in_func, because the in_func code block is included in the print (a) # out_func function in out_func, B cannot be extended to in_func, because in in_func, B is rebound to different objects, therefore, the scope of B in out_func cannot be extended to in_func. B = 1 in Python, the name defined in the class code block can only be visible in the class, and the scope of the class name cannot be extended to the method in the class. If the generator expression and list expansion appear in the class definition, the names of the classes cannot be extended to these expressions, because list expansion and generator expression implementation both use the function scope.

Class C: a = 0 # in the list expression, a will throw NameError due to undefined expression B = list (a + I for I in range (10) def method (self):

 

# Because the name defined in the class cannot be extended to the method, the following statement is incorrect and will throw a non-defined NameError exception print () when a name is used in a code block, the nearest peripheral scope is parsed to find the name. All these scopes visible in the current code block are called the environment of the current code block. Relationship between name binding and scope if a name is bound to a code block, unless the name is declared as nonlocal (The Role of nonlocal declaration is to make the variable in the peripheral scope, otherwise, this name is the local variable of the code block. If a name is bound to the module level, the scope of the name is global. This variable is a global variable (a variable in the module, and a local variable in the module, for the code block in the module, it is a global variable ). If a name is used in a code block but not defined in this Code block, this variable is a free variable. Name binding exception if the name is not found during name search, a NameError exception is thrown. If the name references a local variable, however, if the name is not bound to this local variable, an UnboundLocalError exception is thrown (UnboundLocalError is a subclass of NameError ). When name binding occurs, name binding usually occurs: When a parameter is passed to a function, the parameter name is bound to the passed object and imported using the import Statement, where from... the import * Statement will bind all the names that can be imported to the imported module to the operation class definition. When the function is defined, the value assignment operation will be performed in the for statement in the with statement in the for statement of the for loop. the Pitfall bound by the name of the as in Python after the as statement in the keep CT statement is in Python, some rules for name binding may cause incomprehensible errors when using names, especially for users with C, C ++, and Java experience. In Python, no matter where the name binding operation occurs in the current block, references to this name in this code block will use the objects bound to the current block. The problem arises. If we reference this name before the name binding operation, an error will occur and an UnboundLocalError exception will be thrown.
>>> A = 10 >>> def function (): print (a) a = 20 # The binding operation of a occurs before print >>> function () traceback (most recent call last): File "<pyshell #5>", line 1, in <module> function () File "<pyshell #4>", line 2, in function print (a) UnboundLocalError: local variable 'A' referenced before assignment

 

In Python, local variables in the code block can obtain the bound name by scanning the entire code block. Therefore, in the code above, the name a has been found by scanning the code block during print execution, but the binding operation of name a has not yet occurred, so an error occurs. In the above Code, if we need to define the global variable a, we can use the global statement for declaration.
>>> A = 10 >>> def function (): global aprint (a) a = 20 # the new name is not introduced here, instead, you can bind the global variable a to 20.> function () 10> a20

 

The role of the global statement is to allow reference to objects declared through this statement to use the name in the top-level namespace. In the top-level namespace, the global namespace and the built-in namespace are included. The global namespace is first searched. If it is not found, the internal namespace is searched. The global statement must appear before the name is used. If the free variable in the peripheral scope contains a global declaration, the free variable is considered global. When looking for a built-in namespace, the built-in namespace accesses the _ builtins _ name in the global namespace of the current code block. This name references a name dictionary or a module. In the _ main _ module, the reference of _ builtins _ is the built-in module builtins. However, in other modules, _ builtins _ references the name Dictionary of the builtins module. Note: In the implementation of CPython, the variable _ builtins _ cannot be manually modified. to overwrite the name in the built-in namespace, you must import the builtins module, then modify the corresponding attributes in this module.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.