python 三层装饰器

https://www.jb51.net/article/168276.htm

def logging(level):
    def wrapper(func):
        def inner_wrapper(*args, **kwargs):
            print ("[{level}]: enter function {func}()".format(level=level,func=func.__name__))
            return func(*args, **kwargs)
        return inner_wrapper
    return wrapper

@logging(level='INFO')
def say(something):
    print ("say {}!".format(something))

# 如果没有使用@语法,等同于
# say = logging(level='INFO')(say)

@logging(level='DEBUG')
def do(something):
    print ("do {}...".format(something))

if __name__ == '__main__':
    say('hello')
    do("my work")
def say_hello(contry):
    def wrapper(func):
        def deco(*args, **kwargs):
            if contry == "china":
                print("你好!")
            elif contry == "america":
                print('hello.')
            else:
                return

            # 真正执行函数的地方
            func(*args, **kwargs)

        return deco

    return wrapper


@say_hello('america')
def american():
    print("I am from America.")


@say_hello('china')
def chinese():
    print('我来自于中国')


american()

你可能感兴趣的:(python,python)