Supplementary python function


Using function names, first-class objects

function name can be used like a variable
1. Assignment
2. As a list element
3. As a parameter
4 as a return value



1. The function assignment to variables
DEF main ():
     Print ( ' ! ah, this very handsome young man, beat him up ' ) 

M1 = main    # to function surface assigned to the variable name, so you can use the 
M1 ()       # direct call to 
Print (M1, main )    # function memory address 
Print . (M1 __name__ )   # View original function name 
# output: 
# ah, this very handsome young man, beat him up! 
# <main function 0x000002889B3318C8 AT> <AT 0x000002889B3318C8 main function> 
# main
 
 
2. Make the call as a list of elements
DEF M1 ():
     Print ( ' this beauty pretty, take home ha ha! ' )
 DEF M2 ():
     Print ( ' ah, this handsome, thrown into jail, ' )
 DEF M3 ():
     Print ( ' ah, this, this, too handsome young man, handed over to the police shot go ' )
 DEF M4 ():
     Print ( ' . Well, what can not think of a ' ) 

LST = [M1, M2, M3, M4]
 Print (LST)   # corresponds to printing out these functions are memory addresses 
for I in LST: 
    I ()      # to perform a method of the above functions

LST = [M1 (), M2 (), M3 (), M4 ()] 
LST   # this is a direct function to execute a method 

for i in LST: 
    i    # method this is also a direct execution of the function 
# Note: This above do not ( ) two methods, do not call together, he calls the list on the inside 
# the following code calls the list there will be no value can be called the 
# output: 
# [<function M1 AT 0x00000197FD2A18C8>, <function M2 AT 0x00000197FD2A1950>, 
# <function AT 0x00000197FD33A840 M3>, <function M4 AT 0x00000197FD33A9D8>]
# this beauty pretty, take home! Haha # ah, this handsome, thrown into jail, # ah, this, this, too handsome young man, handed go to the police shot # Well, what can not think. # this beauty pretty, take home! Haha #Ah, this handsome, thrown into jail, # ah, this, this, too handsome young man, handed over to the police shot to # Well, what can not think of.
 

 

3 can also function as a parameter
DEF foo (B1): 
    B1 () 


DEF bar ():
     Print ( ' calls bar ' ) 

foo (bar)     # this as a function of parameters, traditional values, call this function 
# outputs: 
# calls bar
 
 
4. The return value as a function of
DEF function ():
     DEF good ():
         Print ( ' !! Python hard to learn ah should strive to own ' )
     Print (good) # print good function of memory address 

    return good      # return to good function 

RET = function ()     # In the function function , the function returns good 
ret ()    # perform good function 
Print (ret)   # can be seen that a good function ret received because the memory address and the memory address where the same good function 


# outputs: 
# . <function function <about locals> AT 0x000001CAFD481950 .good> 
# Python hard to learn ah! to their own efforts! 
# <function function.<locals>.good at 0x000001CAFD481950>
 

 

# Conjunction with the above summary, the function is a variable.

 

 

 

 

Guess you like

Origin www.cnblogs.com/Pengdachui-1/p/11111877.html