Discuss / Python / 作业_小小献丑

作业_小小献丑

Topic source
import functools
# 一次实现带参数和不带参数效果
def log(text=None):
    def decorator(func):
        if text is not None:
            print('%s %s()' % (text, func.__name__))

        @functools.wraps(func)
        def wrapper(*args, **kw):
            print('begin call')
            f = func(*args, **kw)
            print('end call')
            return f

        return wrapper

    return decorator

承影555

#2 Created at ... [Delete] [Delete and Lock User]

你的方法我试过!可是有点小bug,就是有一个方法会报TypeError: decorator() missing 1 required positional argument: 'func'错误

具体是哪个方法呢?可以贴贴看,func就是装饰的那个方法,怎么会没传过去呢?

zmj27404

#4 Created at ... [Delete] [Delete and Lock User]

我猜是因为两个函数弄成同一个名字造成的


  • 1

Reply