Advanced Functions
1, namespaces - three kinds
# Built-in namespace - python interpreter
# Is the python interpreter to store a name you can start to use the built-in namespace
# Built-in name to be loaded into memory at boot time interpreter
# Global namespace - we write the code but the code is not the function of
# Is in the process of being executed in the program from top to bottom in turn loaded into memory
# Where to put all the variables and function names we set
# Local namespace - function
# Is the internal name of the function definition
# When the called function will produce the name space with the end of the function performed by this namespace disappear again
# Locally: You can use a global, built-in namespace names
# In Global: You can use the built-in namespace name, but can not be used in topical
# Built-in: You can not use the names of local and global
DEF max (L): Print ( ' in FUNC max ' ) Print (max ([l, 2,3])) # in max FUNC # Under normal circumstances, the direct use of the built-in name # if we define the global and when the built-name namespace of the same name, will use a global name # when I myself sometimes I do not find my superiors to the # if he did not find it to be on a higher level if the parent did not find built-in namespace are not on the error # multiple functions should have a more independent local namespace, do not share with each other
2. Scope - two kinds
# Global scope - the whole picture - and built the global namespace names are part of the global scope --globals ()
# Local scope - role in local - function (name local name space belonging to local scope) --locals ()
# Global scope Global A. 1 = DEF FUNC (): Global A A = 2 FUNC () Print (A) # 2 # for immutable data type, but in view of the global scope local variables # but can not directly modify # if you want to modify, you need to add global statement at the beginning of the program # If you declare a global variable in a local (function), then the variable will be global variables are valid in all local operations
# About locals See local variable A =. 1 B = 2 DEF FUNC (): X = ' AAA ' Y = ' BBB ' Print (about locals ()) # View local variable FUNC () Print (about locals ()) # Display global Print (Globals ()) # View the global # Globals always print the name of the global # locals output according to what position where the locals
3, nested functions
DEF max (A, B): return A IF A> B the else B DEF the_max (X, Y, Z): # nested calls the function C = max (X, Y) return max (C, Z) Print (the_max (1,2,3))
3.1, nonlocal usage
A =. 1 DEF Outer (): A =. 1 DEF Inner (): A = 2 DEF Inner2 (): nonlocal A # declare a local variable of the first layer above A =. 1 + # immutable data types of modifications Inner2 () Print ( ' ## ## a: ' , a) # . 3 (inner removed if the function a = 2, the result is 2) inner () Print ( ' ** ** a: ' , a) # . 1 (inner if removed function a = 2, the result is 2) Outer () Print ( ' global: ', A) # . 1 (inner removed if the function a = 2, the result is. 1) # nonlocal local variables can only be used to find the current function from the upper layer of the layer nearest local variable # declare variables inside a function of changes will nonlocal recently affect the function of local variables from the current level of # the global invalid # to only a partial impact on the most recent one
4, function names can be assigned
DEF FUNC (): Print (123 ) FUNC () # function name is the address of the memory 123 # func2 = FUNC # function name can be assigned func2 () # 123
DEF FUNC (): Print (123 ) DEF Wahaha (F): # F () return F # function name as the return value of the function can be qqxing = Wahaha (FUNC) # name can be given as a parameter qqxing ()
5, closure: nested function, the internal variable function calls to external functions
def outer(): a = 1 def inner(): print(a) return inner inn = outer() inn()