关于软件测试中的装饰器

装饰器(Decorator)是Python中一个非常强大且常用的功能,它允许你在不修改原有函数或类代码的情况下,给函数或类增加新的功能。装饰器本质上是一个函数,它接收一个函数作为参数并返回一个新的函数。这个新函数是对原函数的一个增强版本,它可以在调用原函数之前或之后执行一些操作。

装饰器的基本使用

定义一个装饰器

装饰器函数通常接受一个函数作为参数,并返回一个新的函数。下面是一个简单的装饰器示例,它会在被装饰的函数执行前后打印日志:

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.
使用functools.wraps

在上面的例子中,虽然装饰器工作正常,但如果你尝试获取say_hello函数的__name____doc__等属性,你会发现它们现在指向的是wrapper函数而不是say_hello函数。为了解决这个问题,可以使用functools.wraps装饰器来更新wrapper函数,使其看起来更像被装饰的函数。

from functools import wraps
def my_decorator(func):
@wraps(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():
"""This function says hello."""
print("Hello!")
print(say_hello.__name__) # 输出: say_hello
print(say_hello.__doc__) # 输出: This function says hello.

装饰器带参数

如果你的函数需要参数,你可以通过*args**kwargs来让wrapper函数接受任意数量的位置参数和关键字参数,并将它们传递给原函数。

def my_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("Something is happening before the function is called.")
result = func(*args, **kwargs)
print("Something is happening after the function is called.")
return result
return wrapper
@my_decorator
def add(a, b):
return a + b
print(add(5, 3))

输出将会是:

Something is happening before the function is called.
8
Something is happening after the function is called.

装饰器是Python中一个非常灵活和强大的特性,它允许你以非侵入式的方式扩展函数或类的功能。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值