Discuss / Python / 有疑惑

有疑惑

Topic source

涵_天

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


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


@log('1')
def test1(text):
    print(text)


@log('execute')
def test2(text):
   print(text)


test1('1')
test2('2')


为什么@log(这里必须加参数?),如果不加参数,不会报错,但是也不会执行,什么都不打印,函数test1也不会执行。。

你是

@log

还是

@log()

后者也没执行吗?


  • 1

Reply