The function name is a variable , but it is a special variable, with the brackets can perform functions with a variable
1.1. The function name memory address
def func():
print("呵呵")
print(func)
结果: <function func at 0x1101e4ea0>
1.2 function name can be assigned to other variables
def func():
print("呵呵")
print(func)
a = func # 把函数当成一个值赋值给另一个变量
a() # 函数调用 func()
1.3. Element name can be used as containers of
def func1():
print("呵呵")
def func2():
print("呵呵")
def func3():
print("呵呵")
def func4():
print("呵呵")
lst = [func1, func2, func3]
for i in lst:
i()
1.4. The function name can be used as an argument to another function
def func():
print("吃了么")
def func2(fn):
print("我是func2")
fn() # 执行传递过来的fn
print("我是func2")
func2(func) # 把函数func当成参数传递给func2的参数fn.
1.5. Name can be another function as a return value
def func_1():
print("这里是函数1")
def func_2():
print("这里是函数2")
print("这里是函数1")
return func_2
fn = func_1()
# 执行函数1. 函数1返回的是函数2, 这时fn指向的就是上面函数2
fn() # 执行func_2函数