table of Contents:
1, function object
2, the nested function
3, namespace
4. Scope
Functions are first-class objects
1, function names can be cited:
def index(): print('from index') a = index a()
2, the function name can be passed as parameters
def foo(x,y,func): print(x,y) func() def bar(): print('from bar') foo(1,2,bar) result: 1 2 from bar
3, the function name can be used as the return value
Parameter passing when there is no special needs, must not parentheses, brackets executed on the spot
def index(): print('from index') def func(a): return a a=func(index) print(a) # <function index at 0x0000000000451E18> a() # from index
4, the function name can be used as an element of a container type
def func(): print('from func') L1 = [. 1, ' 2 ' , FUNC, FUNC ()] # parameter in the function name and the brackets run directly output FUNC from F = L1 [2 ] Print (F) # <function FUNC AT 0x0000000001D01E18>
def register(): print('register') def login(): print('login') def shopping(): print('shopping') def pay(): print('pay') func_dic={'1':register,'2':login, '3':shopping,'4':pay } def main(): while True: print(""" 1. Registration 2. Log 3. Shopping 4. Payment 5. Quit """) Choice = INPUT ( " Please enter the number corresponding to: " ) .strip () IF Choice == ' . 5 ' : BREAK IF Choice Not in func_dic: Continue the else : func_dic[choice]() main()
Nested function calls: call functions within the function
def index(): print('from index') def func(): index() print('from func') func()
def func1(x,y): if x>y: return x else: return y def func2(x,y,z,a): result=func1(x,y) result=func1(result,z) result=func1(result,a) return result print(func2(1,2,454,7)) #454
Nested function is defined:
def index(): def home(): print('from home') home() index() #from home
Term space
What is the name space?
Space for the name
if you want to access a variable value, it must first direction corresponding namespace binding relationship to get the name and address of the corresponding memory
Category namespace:
1, built-in namespace:
Python in advance to give you the definition of complete name, is the presence of built-in namespace
2, global name space
stored in a file-level name is a global name space
if while for internally defined name execution after all stored in the global name space
3, the local name space
for all internal functions defined names are stored in the current function of the built-in namespace
Life Cycle:
1, built-in namespace
when the python interpreter started to take effect, shut down the interpreter when failure
2, global name space
when you start the current the py file take effect after the current end of the page code execution failures
3, the local name space
take effect when you call the current function, the function body end of the last line of code is executed on the failure
def index(): X =. 1 # return X Print ( " index1, " , index) # global spatial print global space function index1 <function index AT 0x0000000002061E18> DEF foo (): Print ( " index2 " , index) # local spaces call global variable index1 <function AT 0x0000000002061E18 index> foo () Print ( Print ) # global space inner space print function <built-in function print> DEF index (): Print ( Print ) # local spatial inner space print function index () # <Built-in function Print>
if 1 == 1: X =. 1 # global space print(x) while True: a = 2 break Print (A) # global space for I in Range (2 ): Print (I) Print (i) # . 1 to print the last value of i
Namespace search order:
Local: Local> Global> Built-in
Global: Global> Built-# built on the error and then could not find
the name of an internal function used in the definition phase has been specified dead, nothing to do (with your call position relationship between the variables in the local, nested functions We will look for the top; if it is a function call each other, independent of other variables function variables )
x = 111 def func1(): x = 222 def func2(): the X- = 333 DEF func3 (): # the X-444 = DEF the Func4 (): # the X-555 = Print (the X-) # call nesting recent x, = 333 is the X- Print ( ' from the Func4 ' ) func4 () func3() func2() func1 () # 333
x = 1 def wrapper(): x = 2 index() DEF index (): # the X-3 = Print (the X-) # can not find x, to find global variable wrapper () # 1
x = 1 def inner(): x = 2 def wrapper(): print(x) wrapper() inner() # 2
x = 1 DEF index (X = Arg): # defined time, arg is assigned to a. 1 Print (X) Print (Arg) x = 2 index() ''' 2 1 '''
x = 111 def index(): def wrapper(): print(x) return wrapper # return wrapper index() x = 222 f = index() f() # 222
Classification scope:
1, the global scope of
a global name can be invoked to exist in the global scope
Built-in global namespace namespace +
2, local scope
locally can call the name stored with local scope
local name space
global: declare global variables (***)
nonlocal: local namespace declarations of local variables, local variables modify the upper function (*)
Only variable types can modify the local value of the external variable (*****)
x=1 DEF index (): , Ltd. Free Join the X- # can not write = the X-2, Ltd. Free Join the X-2 = index() print(x) #2
x=111 def index(): x=1 def func2(): x = 2 def func(): X nonlocal # local variables associated with the upper layer, the upper layer modify the local variable X X =. 3 func() Print (X) # . 3 func2 () Print (X) # . 1 index () Print (X) # 111
Review # local variables can not affect the upper layer, the upper layer
def index(): x=1 def index2(): nonlocal x x=2 index2() print(x) index()
** variable types can modify the value of external variables in the local
l1=[] def index(a): l1.append(a) print(l1) #[[...]] index(l1) print(l1) #[[...]]
def func1(): l1=[1,2,3,4,5] def func2(): l1.append(6) return func2() print(l1) # [1, 2, 3, 4, 5, 6] func1 () print(l1) # [8, 8, 8]