Python function objects

First, the function object 
essence: can function as a variable to use
func = memory address
DEF func ():
     Print ( ' func func ' ) 

# Note func, and func () is the difference? 
#    FUNC is a variable name, alias equivalent memory address. 
#    FUNC () function is invoked, get a result. 

# 1, assignment: 
func
 # F # = func func is to assign a variable memory address 
# Print (F, func) # <0x000001C31B2895E0 function func AT> <AT 0x000001C31B2895E0 function func> 
# F1 = func () is # the FUNC () result to a variable 
# Print (F1) None # 
# Print (F ()) None FUNC FUNC # 
# Print (FUNC ()) FUNC FUNC # # None
# 2, can be passed as a parameter function 
DEF foo (X):    # X = FUNC memory address 
    # Print (X) # <function FUNC AT 0x000001D9F8E795E0> 
    # same effect following 
    X () 
    FUNC () 

# A = 111 
# foo (111) 
# foo (a) 
# FUNC memory address as a parameter passed in 
foo (FUNC)   # foo (FUNC memory address)

 

 

# 3, as the return value as a function of another function 
# DEF foo (X): X = FUNC # memory address 
#      return return FUNC # X memory address 
# RES = foo (FUNC) # foo (FUNC of memory address) 
# Print (RES) # <function FUNC AT 0x000001BA694195E0> 
# 
# after res () # were added () results obtained: FUNC FUNC 

# . 4, can be used as a container element type. 
# Example 1: List 
# L = [FUNC,] 
# Print (L) # [<function FUNC AT 0x000002F8227195E0>] 
# obtain the list of elements following 
# L [0] 
# call list 
# L [0] () # FUNC FUNC 

# example 2: dictionary 
# DIC = { 'K1':} FUNC 
# print(dic)  # {'k1': <function func at 0x00000266D75895E0>}
# 调用
# dic['k1']()  # func func

 















Guess you like

Origin www.cnblogs.com/liunaixu/p/12606283.html
Recommended