能否写出一个@log的decorator,使它既支持:@log()又支持@log('execute')

题目:能否写出一个@log的decorator,使它既支持:@log()又支持@log('execute')

'''

能否写出一个@log的decorator,使它既支持:@log()又支持@log('execute')

import functools
def log(*text):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args,**kw):
            print(func.__name__,' begin ' ,*text)
            func(*args,**kw)
            print(func.__name__,' end ',*text)
            
        return wrapper
    return decorator

@log('execute')  #or@log()
def foo():
    print('foo is running...')
if __name__=='__main__':
    f=foo
    f()

'''

你可能感兴趣的:(能否写出一个@log的decorator,使它既支持:@log()又支持@log('execute'))