装饰器是Python中非常重要的概念,它可以用于修改或扩展函数或类的功能。本文将详细讲解Python中装饰器的所有用法,包括函数装饰器、类装饰器、装饰器链、带参数的装饰器等。下面对这些用法进行详细讲解:
1. 函数装饰器
1.1 基本用法
函数装饰器是最常见的装饰器类型,它可以用于修改或扩展函数的功能。例如:
def my_decorator(func):
def wrapper():
print('Before function call')
func()
print('After function call')
return wrapper
@my_decorator
def say_hello():
print('Hello')
say_hello()
在上面的代码中,我们定义了一个名为my_decorator
的函数装饰器,它接受一个函数作为参数,并返回一个新的函数。我们使用@my_decorator
语法将say_hello
函数传递给my_decorator
装饰器,并将返回的新函数赋值给say_hello
函数。最后,我们调用say_hello
函数,输出结果为:
Before function call
Hello
After function call
在输出结果中,我们可以看到my_decorator
装饰器在say_hello
函数调用前后分别输出了Before function call
和After function call
。
1.2 带参数的装饰器
我们可以使用带参数的装饰器来扩展装饰器的功能。例如:
def repeat(num):
def my_decorator(func):
def wrapper():
for i in range(num):
func()
return wrapper
return my_decorator
@repeat(num=3)
def say_hello():
print('Hello')
say_hello()
在上面的代码中,我们定义了一个名为repeat
的带参数装饰器,它接受一个参数num
,并返回一个新的函数装饰器。新的函数装饰器接受一个函数作为参数,并返回一个新的函数。新的函数会重复调用原函数num
次。我们使用@repeat(num=3)
语法将say_hello
函数传递给repeat
装饰器,并将返回的新函数赋值给say_hello
函数。最后,我们调用say_hello
函数,输出结果为:
Hello
Hello
Hello
在输出结果中,我们可以看到repeat
装饰器重复调用了say_hello
函数3次。
2. 类装饰器
除了函数装饰器,Python还支持类装饰器。类装饰器可以用于修改或扩展类的功能。例如:
class my_decorator:
def __init__(self, func):
self.func = func
def __call__(self):
print('Before function call')
self.func()
print('After function call')
@my_decorator
def say_hello():
print('Hello')
say_hello()
在上面的代码中,我们定义了一个名为my_decorator
的类装饰器,它接受一个函数作为参数,并重载了__call__
方法。我们使用@my_decorator
语法将say_hello
函数传递给my_decorator
装饰器,并将返回的新函数赋值给say_hello
函数。最后,我们调用say_hello
函数,输出结果为:
Before function call
Hello
After function call
在输出结果中,我们可以看到my_decorator
装饰器在say_hello
函数调用前后分别输出了Before function call
和After function call
。
3. 装饰器链
我们可以使用多个装饰器来扩展函数或类的功能。例如:
def my_decorator1(func):
def wrapper():
print('Before function call 1')
func()
print('After function call 1')
return wrapper
def my_decorator2(func):
def wrapper():
print('Before function call 2')
func()
print('After function call 2')
return wrapper
@my_decorator1
@my_decorator2
def say_hello():
print('Hello')
say_hello()
在上面的代码中,我们定义了两个函数装饰器my_decorator1
和my_decorator2
,它们分别输出Before function call
和After function call
。我们使用@my_decorator1
和@my_decorator2
语法将say_hello
函数传递给这两个装饰器,并将返回的新函数赋值给say_hello
函数。最后,我们调用say_hello
函数,输出结果为:
Before function call 1
Before function call 2
Hello
After function call 2
After function call 1
在输出结果中,我们可以看到my_decorator1
和my_decorator2
装饰器分别在say_hello
函数调用前后输出了Before function call
和After function call
。
4. 示例说明
下面是两个使用以上方法的示例说明:
示例1:使用函数装饰器扩展函数功能
def my_decorator(func):
def wrapper():
print('Before function call')
func()
print('After function call')
return wrapper
@my_decorator
def say_hello():
print('Hello')
say_hello()
在上面的代码中,我们使用函数装饰器my_decorator
扩展了say_hello
函数的功能,输出结果为:
Before function call
Hello
After function call
示例2:使用类装饰器扩展类功能
class my_decorator:
def __init__(self, func):
self.func = func
def __call__(self):
print('Before function call')
self.func()
print('After function call')
@my_decorator
def say_hello():
print('Hello')
say_hello()
在上面的代码中,我们使用类装饰器my_decorator
扩展了say_hello
函数的功能,输出结果为:
Before function call
Hello
After function call