Python 装饰器:如何改进代码的功能和可读性
什么是装饰器?
用装饰器来扩展另一个函数的功能。通过使用装饰器,可以添加其他功能。
为了定义装饰器,需要定义一个函数,该函数具有一个调用 wrapper 的函数和一个调用 func, 的参数,该参数是我们将用于增加功能的函数。该 wrapper 函数采用 func 具有的所有参数。
将在我们的 wrapper 函数中使用 func 。通过这样做,可以在 func 函数之前和之后进行操作。
若要使用修饰器,请先定义一个函数(或类方法),然后使用 @ 符号将修饰器应用于该函数。
使用装饰器
为了在示例中使用装饰器,将使用:
- func_decorator:返回一个包装函数
- wrapper:个是内部函数(这个函数会采用func所有的参数)?
- func: wrapper 函数中使用的函数的参数。
- other_function:在装饰器中使用的其他一些功能。
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
以下代码的输出为:
Something is happening before the function is called.
Hello!
Something is happening after the function is called.
正如所看到的,我们正在使用装饰器,其中们用另一个称为包装器的函数定义一个函数,我们使用“@” wrapper 函数在要使用的进程之间运行它。通过这样做,可以在 func 函数之前和之后进行操作。
使用带有参数的装饰器
将参数与装饰器一起使用非常简单。只需要向 wrapper 函数添加参数。例如:
def my_decorator(func):
def wrapper(name):
func()
print(name)
return wrapper
@my_decorator
def say_hello():
print("Hello ", end="")
say_hello("Sarper")
# OUTPUT
# Hello Sarper
代码示例
class methods 和 static methods 的例子。
类方法和静态方法是在类中指定的两种类型的方法,但在面向对象编程中无需生成类实例即可访问。
静态方法
static method 是属于类但无权访问实例或类状态的那些。在 Python 中,它是使用 @staticmethod 装饰器定义的。与类方法相比,静态方法不需要任何引用类或实例的参数。
class Numbers:
def __init__(self, num):
self.num = num
@staticmethod
def add(a, b):
return a.num + b.num
@staticmethod
def sub(a, b):
return a.num - b.num
@staticmethod
def mul(a, b):
return a.num * b.num
@staticmethod
def div(a, b):
return a.num / b.num
a = Numbers(6)
b = Numbers(3)
print(Numbers.add(a, b))
print(Numbers.sub(a, b))
print(Numbers.mul(a, b))
print(Numbers.div(a, b))
# OUTPUT
# 9
# 3
# 18
# 2.0
Class 方法
A class method 是特定于类的方法,不适用于任何类实例。它可以更改适用于类的所有实例的类状态,并可通过类名访问。在 Python 中,类方法是使用装饰器声明的 @classmethod 。类方法的第一个参数通常称为 cls ,它代表类名。
class MyClass:
class_variable = 0
@classmethod
def increment(cls):
cls.class_variable += 1
MyClass.increment() # calls the class method
print(MyClass.class_variable) # prints 1
装饰器示例
def smart_divide(func):
def wrapper(a, b):
print(f"{a} divided by {b}")
if b == 0:
print("Can't divide by 0")
return func(a, b)
return wrapper
@smart_divide
def divide(a, b):
print(a/b)
divide(10, 2)
# OUTPUT
# 10 divided by 2
# 5.0
将装饰器与类一起使用
Python 中的装饰器也可用于装饰类。类修饰器(如函数修饰器)允许您在不更改类源代码的情况下更改类的行为。若要创建类装饰器,请定义一个函数,该函数接受原始类作为参数,并生成一个扩展原始类行为的新类。
将类属性添加到类的类装饰器示例如下所示:
def class_attribute_decorator(cls):
cls.new_attribute = "This is a new class attribute."
return cls
@class_attribute_decorator
class MyClass:
pass
print(MyClass.new_attribute)
使用多个装饰器
也可以使用具有一个功能的多个装饰器。例如,您可以像这样使用:
def smart_divide(func):
def wrapper(a, b):
print(f"{a} divided by {b}")
if b == 0:
print("Can't divide by 0")
return func(a, b)
return wrapper
def operation(func):
def wrapper(a, b):
print("New Operation")
return func(a, b)
return wrapper
@smart_divide
@operation
def divide(a, b):
print(a/b)
divide(10, 2)
# OUTPUT
# 10 divided by 2
# New Operation
# 5.0