Discuss / Python / 结合:1、能在函数调用的前后打印出'begin call'和'end call'的日志;2、log可带参数也可不带参数

结合:1、能在函数调用的前后打印出'begin call'和'end call'的日志;2、log可带参数也可不带参数

Topic source

#1 Created at ... [Delete] [Delete and Lock User]
import functoolsdef log(*text):    def decorator(func):        @functools.wraps(func)        def wrapper(*args, **kw):            print('begin call')            if len(text) == 0:                pass            else:                print('%s %s():' % (text[0], func.__name__))            res = func(*args, **kw)            print('end call')            return res        return wrapper    return decorator@log()def now():    print('2022-5-27')@log('execute')def now2():    print('2022-5-27(2)')now()now2()

  • 1

Reply