Function advanced
1 # def func (): # Print (a) # func () # The variables defined outside the function can be used inside the function, # variables defined inside the function cannot be used outside the function # def func () : #1# Func () # Print (a) # error
Namespaces and Scopes
Built-in namespaces-Python interpreter
The Python interpreter stores the names that can be used in the built-in namespaces as soon as it starts
The built-in name is loaded into memory when the interpreter is started.
Global namespaces-code outside of functions we write
is when the program is loaded into memory in sequence from top to bottom.
All the variables and function names we set are placed
Local namespaces--functions
is the name defined inside the function.
This namespace is generated when the function is called, and as the function executes, the namespace disappears.
In the Local: You can use the built-in, global namespace name
In global: You can use names from built-in namespaces, but you cannot use names in local namespaces
In the built-in: the names of local and global namespaces cannot be used
# def Max (): # Print ('In max func') # max () # Def MAX (l): # Print ('In max func') # Print (Max ([1,2,3]) # Under normal circumstances, use the built-in name directly, # When we define the name in the global definition and in the built-in space, we use the global name # when you have some time, do not find their own superiors to the # if you do not have to find the superior, the superior did not find a level, Built-in namespaces do not have a direct error # Multiple functions with this have multiple independent local namespaces, do not share with each other # Def input (): # Print ('In input now!') # # def func (): # input () # Call input () # func () # def func (): # # Input=1# Print (func) # func () #---><function func at0x000001b08d6fc1e0># def func1 (): # a=1# def FUNC2 (): # Print (a) # Two local namespaces are independent of each other # func-memory address of function # function name ()-function's call # function memory address ()--Function invocation
Scope
Global scope--the names in the global and global namespaces are global scope---globals ()
Local scope--------functions (names in local namespaces belong to local scopes)--locals ()
# a =1# def func (): #Globala# a+=1# func () # Print (a) # for non-volatile data types, you can see the variables in the global scope in the local area # but not directly, # If you want to modify it, you need to add it at the beginning of the programGlobalDeclaration # If a global variable is declared within a local (function), then all operations on the local variable will be valid for global # a=1# b=2# def func (): # x='AAA'# y='BBB'# Print (Locals ()) # func () # Print (Globals ()) # def func (): # x='AAA'# y='BBB'# Print (Locals ()) # Print (Globals ()) # func () # Print (Locals ()) # Local Local in local is local locally is global # Print (Globals ()) # Glo Bals always print the global name # Locals output what is based on the location of the locals
Nested and scoped chains of functions
Nested calls to functions
# def Max (A, B): # return if Else b# def the_max (x, Y, z): # nested invocation of function # = max (x, y) # return max (c, z) # Print (The_max (123) )
Nested Definitions of functions
# a =1# DEF outer (): # a=1# def inner (): # a=2# Print (a) # print ('Inner') # def inner2 (): # nonlocal A # declares an upper first-level local variable # a+=1# Changes to immutable data types # print ('Inner2') # inner2 () # Print ('# #a # #', a) # inner () # Print ('**a**', a) # outer () # Print ('Global', a) # nonlocal can only be used for local variables, to find the upper level of the current function of the nearest layer of local variables # declares that the nonlocal of the intrinsic function of the variable modification will affect the upper layer from the current function of the nearest level of local variables # for the global invalid local also only the most recent layer has an impact
The nature of the function name: the memory address of the function
# a =1# DEF outer (): # a=1# def inner (): # a=2# Print (a) # print ('Inner') # def inner2 (): # nonlocal A # declares an upper first-level local variable # a+=1# Changes to immutable data types # print ('Inner2') # inner2 () # Print ('# #a # #', a) # inner () # Print ('**a**', a) # outer () # Print ('Global', a) # nonlocal can only be used for local variables, to find the upper level of the current function of the nearest layer of local variables # declares that the nonlocal of the intrinsic function of the variable modification will affect the upper layer from the current function of the nearest level of local variables # for the global invalid local also only the most recent layer has an impact
Python function advanced