函数返回值是函数
"""
return func in func
"""
# 可调用对象
def hi(name = "yasoob"):
def greet():
return "now you are in the greet function"
def welcome():
return "now you are in the welcome function"
if name == "yasoob":
return greet
else:
return welcome
#a指向的是greet函数(的内存地址)
a = hi()
print(a)
# a函数调用本质是调用greet函数
print(a())
# b指向的是hi函数(的内存地址)
b = hi
# b(name='bidu')返回值是welcome函数地址
print(b(name="bidu"))
# b(name='bidu')()是一次函数调用,welcome函数发生调用
print(b(name = "bidu")())
小结
Python装饰器本质也是返回一个函数地址,然后运行时发生函数调用
网友评论