当前位置: 移动技术网 > IT编程>脚本编程>Python > idata中的装饰器wrapper如何继承原函数名?(python )

idata中的装饰器wrapper如何继承原函数名?(python )

2017年12月28日  | 移动技术网IT编程  | 我要评论

天空小小岛,情挑四十,婚礼车

下面是python ddt模块的一段源码,请教下idata中的装饰器wrapper如何继承原函数名?

def data(*values):
    """
    Method decorator to add to your test methods.

    Should be added to methods of instances of ``unittest.TestCase``.

    """
    return idata(values)


def idata(iterable):
    """
    Method decorator to add to your test methods.

    Should be added to methods of instances of ``unittest.TestCase``.

    """
    def wrapper(func):
        setattr(func, DATA_ATTR, iterable)
        return func
    return wrapper #返回的函数名为wrapper,与func传入不一致

按一般写法,wrapper之前应该还有def decorator(func),然后通过@wraps(func)保留原函数名,可是文中参数func直接在wrapper中,该如何处理呢?

已尝试直接添加@wraps(func)会提示func未定义,改写为一般形态decorator,似乎函数功能又偏离了预期,求教

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网