Discuss / Python / 我这边的代码

我这边的代码

Topic source

lubang03

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

def log(text = None):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            if text is None:
                print('before call %s()' % func.__name__)
            else:
                print('before %s %s()' %(text, func.__name__))    
            result = func(*args,**kw)
            if text is None:
                print('after call %s()' % func.__name__)
            else:
                print('after %s %s()' %(text, func.__name__))    
        return wrapper
    return decorator

@log('execute')
def now():
    print('2017-06-20')

now()

  • 1

Reply