1. Scope
The scope in Python defines the visibility of the name in the code block . If a local variable is defined in a code block, the scope of the local variable is the code block in which it resides. If this definition occurs within the function body, the scope of the variable extends to any block of code contained in the function, but if it is contained within a block of code in this function, the same name is bound to a different object, (A new object is created and a new name is bound) the outside name will not be extended to this block of code.
2. Name binding and scope relationships
If a name is bound to a block of code, unless the name is declared as nonlocal(the function of the nonlocal declaration is to make the variable in the perimeter scope, before the global scope is parsed), otherwise the name is the local variable of the code block. If a name is bound to a module level, the scope of the name is global, which is a global variable (a variable in a module, a local variable for a module, and a global variable for a block of code in a module). If a name is used in a block of code, but is not defined in this block of code, then the variable is a free variable .
The behavior of name binding occurs mainly in:
- The parameter name is bound to the object passed in when the argument is passed to the function normally
- When importing using the import statement, where the
from ... import *
statement binds all the names in the imported module that can be imported
- Class definition.
- When the function is defined
- When you do an assignment
- In the for statement for the For Loop
- After as in the WITH statement
- After as in the expect statement
The trap of name binding in 3.Python
In Python, some rules of name binding result in incomprehensible errors when using names, especially for users with C, C + +, and Java experience.
In Python, a name-binding operation occurs anywhere in the current block, and a reference to that name in that code block uses the object that is bound in the current block. So, the problem is, if we reference the name before the name-binding operation occurs, then an error occurs and throws a unboundlocalerror exception.
>>> A = 10>>>deffunction ():Print(a) a= 20#A's bind operation occurs before print>>>function () Traceback (most recent call last): File"<pyshell#5>", Line 1,inch<module>function () File"<pyshell#4>", Line 2,inchfunctionPrint(a) unboundlocalerror:local variable'a'Referenced before assignment
In Python, a local variable in a block of code can be used to scan the entire block of code to get the name of the binding, so in the above code, the name of a is found by scanning the block of code while executing print, but the bind operation for name A has not yet occurred, so an error has occurred.
In the above code, if we need the global variable a defined outside, you can declare it using the global statement.
def function (): Global a Print (a) = # # This does not introduce a new name, but instead binds the global variable A to 20 >>> function ()10>>> a20
The purpose of the global statement is to make a reference to the object declared by this statement, using the name in the top-level namespace. in the top-level namespace, contains the global namespace and the built-in namespace, the global namespace will be searched first, if not found, will be built into the namespace search . The global statement must appear before the name is used.
If a free variable in the perimeter scope contains a global declaration, then this free variable is considered global.
4. Built-in namespaces
When looking for a built-in namespace, it accesses the __builtins__ name in the global namespace of the current code block, which refers to a dictionary of names or a module. In the __main__ module, the __builtins__ reference is the built-in module builtins, however, if it is in another module, __builtins__ refers to the The name Dictionary of the builtins module.
Print (Globals ())
{'__name__':'__main__','__doc__': None,'__package__': None,'__loader__': <_frozen_importlib_external. Sourcefileloader object at 0x000000000211b0f0>'__spec__': None,'__annotations__': {},
'__builtins__ ': <module ' builtins ' (built-in) >,'__file__':'c:/users/administrator/pycharmprojects/15group/level_2/day2/core/mm/aaad.py','__cached__': None}
Import Builtins Print (Builtins.abs ( -2)) # built-in modules can call in-build methods
Attention:
In the implementation of CPython, you cannot manually modify the __builtins__ variable, and if you need to overwrite the name in the built-in namespace, you need to import the builtins module and then modify the corresponding attribute in the module.
Learn from: http://www.cnblogs.com/now-fighting/p/4622453.html
Python name bindings and scope relationships??