风-fmgao

导航

装饰器

# 装饰器
import logging


# def user_logging(func):
#     def wrapper(*args, **kwargs):
#         logging.warning('%s is running' % func.__name__)
#         return func(*args, **kwargs)
#     return wrapper
#
#
# def bar():
#     print('i am bar')
#
#
# bar = user_logging(bar)
# bar()


def user_logging(level):
    def decorator(func):
        def wrapper(*args, **kwargs):
            if level == 'warn':
                logging.warning('%s is running' % func.__name__)
            return func(*args, **kwargs)
        return wrapper
    return decorator


@user_logging(level='warn')
def bar(name='bar'):
    print('i am bar %s' % name)


bar()

posted on 2018-05-22 10:57  风-fmgao  阅读(140)  评论(0编辑  收藏  举报