Discuss / Python / 题目

题目

Topic source

hcy12321

#1 Created at ... [Delete] [Delete and Lock User]
def logger(textorfunc):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            begin = 'begin call %s'
            typeandempty = not isinstance(textorfunc, str) or textorfunc == ''
            begin = begin if typeandempty else textorfunc + ' ' + begin
            print(begin % func.__name__)
            ret = func(*args, **kw)
            end = 'end call %s'
            end = end if typeandempty else textorfunc + ' ' + end
            print(end % func.__name__)
            return ret
        return wrapper
    if isinstance(textorfunc, str):
        return decorator
    return decorator(textorfunc)


@logger
def print1():
    print('test1')

@logger('execute')
def print2():
    print('test2')


print1()
print2()

  • 1

Reply