Discuss / Python / 有点绕

有点绕

Topic source

Othershe

#1 Created at ... [Delete] [Delete and Lock User]
import functools

def log(text=''):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            print("%s begin call %s()" % (text, func.__name__))
            ret = func(*args, **kw)
            print("%s end call %s()" % (text, func.__name__))
            return ret
        return wrapper
    return decorator

会出一点问题......ret函数的调用结果会在print("%s end call %s()" % (text, func.name))之后显示出来

......刚才说错了,ret不是函数,但是我那个意思你应该懂。


  • 1

Reply