Nested functions, namespaces and scope

Nested function definition

# Concept: to define another function inside a function 

# Why have nested function definitions: call other functions inside the function 
# 1) function fn2 want to use local variables directly fn1 function, fn2 can be defined directly to the fn1 internal, so you can directly access fn1 fn2 variable 
# 2) becomes a function of fn2 name of a local variable fn1, normally used only in the fn1, you want to use in an external, fn2 function object can be used as the return value of the function fn1 
# 3) is also variable with the same name as the function fn1 fn2 receiving external execution result (return value of the function fn1), i.e. it can be called fn2 external

Namespaces

# Namespaces: storage memory address where the variable name and variable value obtained binding relationship, can be traversed to find Python interpreter, using stack storage 
# To access a variable's value must go get the corresponding namespace variable name can access the value of 
# three kinds namespace 
# built-in: built-in namespace; system-on-one; with the interpreter to execute and produce, while the interpreter to stop the destruction 
# , Ltd. Free Join: global namespace; file level, more ; with the file load belongs produce, file has finished running and destruction 
# local: the local name space; function-level, more; with the implementation of your function to generate the function is finished and destroyed 

# Note: file if, while, for, these can be produced with reduced keyword does not produce the namespace 
# load order: built-in built-in namespace> global global name space> local local name space

Scope

# Scope: variable (name | Object) to find and work scope 
# four kinds of scope LEGB # 
# Built-in: Built-Scope: The name is visible in all positions, all parts of the role of all the files 
# , Ltd. Free Join: global scope : name visible anywhere in the current file, the role of the current file of all places 
# Enclosing: nested scopes: the name of the local part and the internal function of the outer function is visible, the role of the current function has been nested inside a function 
# local: local scope: the name is only visible locally, the role of the current function inside 

# Note: 
# between different scopes name does not conflict, in order to achieve reuse the name of 
# each scope priority to use their own name in scope, did not find greater the name of the namespace until built, not on the error 
# search order: local local scope> Enclosing nested scopes> global global scope> built-in built-in scope

Function name search order

Find the name of the order
     need to first determine where you currently 
        stand in the overall 
            global namespace > built-in namespace 
        standing local 
            local name space > global namespace> built-in namespace

Function in order to find the definition phase has been determined that the name will not change as a function of position and call the change
notice of the name must be created can not be used to create the first in a namespace lookup before use, otherwise an error

 

global keyword

# Action: the local variable to a global variable 
# 1 is not the same name as a global variable, directly enhance the local variables as global variables 
# 2 has the same name as a global variable, is to unify global and local variables of the same name 
#        - If you want to change the overall situation of local the value of the variable (the address change occurs), declare that a plurality of variable global variable separated by commas in a global variable to modify

nonlocal keyword

# Action: the local variable lift nested local variable 
# 1 must have the same name as a local variable nested, is to unify the nested local variable with the same name as the local 
#        - If you want to change the value of the local nested local variables (occurrence address changes), you can declare the variable with nonlocal nonlocal variables If you want to modify multiple comma-separated variable

 

 

 

Guess you like

Origin www.cnblogs.com/george-007/p/11165089.html