目录
装饰器是修改其他函数功能的函数,有利于让我们的代码更简短,也更Pythonic(Python范儿)。
一切皆对象
def hi(name="tom"):
return "hi " + name
print(hi()) # hi tom # 删除之后也无法正常打印
# 我们甚至可以将函数赋值给一个变量,比如:
greet = hi
# 注意:这里没有小括号,因为不是在调用hi函数
# 而是将它放在greet变量里面
print(greet()) # hi tom
# !!如果我们删掉旧的hi函数,将会发生什么!!
del hi
print(hi()) # NameError报错
print(greet()) # hi tom
在函数中定义函数
def hi(name="tom"):
print("now you are inside the hi() function")
def greet():
return "now you are inside the greet() function"
def welcome():
return "now you are inside the welcome() function"
print(greet())
print(welcome())
print("now you are inside the hi() function")
hi() # 无论何时调用hi(),greet()和welcome()将会被同时调用
# now you are inside the hi() function
# now you are inside the greet() function
# now you are inside the welcome() function
# now you are inside the hi() function
# greet() # NameError ——greet()和welcome()函数在hi()函数之外不能访问
从函数中返回函数
def hi(name="tom"):
def greet():
return "now you are inside the greet() function"
def welcome():
return "now you are inside the welcome() function"
if name == "tom":
return greet
else:
return welcome
a = hi()
print(a) # <function hi.<locals>.greet at 0x000001A02DD87420>
# 也就是a指向hi()函数中的greet()函数
print(a()) # "now you are inside the greet() function"
上述代码中,在if/else语句中我们返回greet和welcome,而不是greet()和welcome().为什么呢?
是因为当我们把一对小括号放在后面,函数就会执行;如果没有括号,它就会被传递,赋值给别的变量而不去执行
将函数作为参数传给另一个函数
def hi():
return "hi tom!"
def doSomethingBeforeHi(func):
print("I am doing some boring work before executing hi()")
print(func())
doSomethingBeforeHi(hi)
# I am doing some boring work before executing hi()
# hi tom!
你的第一个装饰器
在上一个例子中,我们已经创建了一个装饰器,现在我们修改一下上一个装饰器,并编写一个稍微有用点的程序:
def a_new_decorator(a_func):
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
def a_function_requiring_decoration():
print("I an the function which needs some decoration to remove my foul smell")
a_function_requiring_decoration()
# "I an the function which needs some decoration to remove my foul smell"
a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
a_function_requiring_decoration()
# "I am doing some boring work before executing a_func()"
# "I an the function which needs some decoration to remove my foul smell"
# "I am doing some boring work after executing a_func()"
上述代码应用了之前学习到的原理,它封装了一个函数
下面使用@来运行之前的代码:
def a_new_decorator(a_func):
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
@a_new_decorator
def a_function_requiring_decoration():
print("I an the function which needs some decoration to remove my foul smell")
a_function_requiring_decoration()
# I am doing some boring work before executing a_func()
# I an the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)