A namespace
# Function Advanced # A. 1 = # DEF FUNC (): # Print (A) # FUNC () # namespace and scope # Print () # INPUT () # List # tuple # namespaces three # built namespace - python interpreter # is the python interpreter to store a name you can start to use the built-in namespace # built-in name at the time of starting the interpreter is loaded into memory # global namespace - we write the code, but not function code # is in the process of execution of the program from top to bottom in order to be loaded into memory # placed all variables and function names we set # local namespace - function # is the name of a function defined inside # when calling when the function will produce this namespace with the end of this namespace function to execute disappear again #Local: You can use a global, built-in namespace names # in Global: You can use the built-in namespace name, but can not use topical use # built-in: You can not use local and global name # DEF FUNC (): # A. 1 = # # FUNC () # Print (A) # DEF max (L): # Print ( 'max in FUNC') # # Print (max ([l, 2,3])) # under normal circumstances directly using the built-name # when we defined and built-in namespace of the same name in the global name, will use a global name # when I myself sometimes I do not find my superiors to the # if we are not to find on one level there is no one to find on a built-in namespace if you are not being given # more functions should have a more independent local namespace, do not share with each other # DEF the INPUT (): # Print ( 'in now the INPUT ') #FUNC DEF (): # # =. 1 INPUT # Print (INPUT) # FUNC () # DEF fun1 (): # A. 1 = # # DEF fun2 (): Pass # FUNC -> memory address Function # Function Name ( ) call # memory address of the function () function is called # scope two kinds # global scope - the whole picture - and the name of the built-in global namespace belong to the global scope --globals () # local scope - role in local - function (local namespace name belonging to local scope) --locals () # a. 1 = # DEF FUNC (): # Global a # a = 2 # # FUNC () # Print (A) #For immutable data types, but watch variables in the global scope of the local # 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 within a local (function) a global variable, this variable will have global variables are valid in all operating local # a. 1 = # B = 2 # DEF FUNC (): # X = 'AAA' # Y = 'BBB' # Print (about locals ()) # Print ( Globals ()) # # FUNC () # Print (Globals ()) # Print (locals ()) # local # Globals always print the global name # locals output what the position of locals where # a = 1 # DEF FUNC ( A): # A = 2 # return a # # a = func(a) # print(a)
Two, nested and scope chain
# DEF max (A, B): # return IF A A> B the else B # # DEF the_max (X, Y, Z): nested function calls # # C = max (X, Y) # return max (C , Z) # # Print (the_max (l, 2,3)) # nested defined function # internal variable function using an external function # 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) # Inner () # Print ( '** ** A:', A) # Outer () # Print ( 'Global:', A) # nonlocal only for local variables to find the upper layer from the current function nearest local variable layer of # variables declared within the function of nonlocal changes will affect the function of the recent local variables from the current level of # the global invalid # for local only to the most recent one influential # A = 0 # DEF Outer (): # # A. 1 = # DEF Inner (): # # 2 = A # DEF Inner2 (): # nonlocal A # Print (A) # Inner2 () # Inner () # # # Outer () # DEF FUNC (): # Print (123) # # # FUNC () # function name is the memory address # func2 = FUNC # name can be assigned # func2 () # # L = [ func, func2] # function name can be used as a container type of element # Print (L) # for I in L: # I () DEF FUNC (): Print (123 ) DEF Wahaha (F): F () return F # function name as the function return value qqxing = Wahaha (FUNC) # name can be given as a parameter qqxing ()