装饰器

一个函数(simple_decorator)内部定义另外一个函数(wrapper),然后返回一个新的函数(f)
def simple_decorator(f):
    def wrapper():
        print("func enter")
        f()
        print("func exit")
    return wrapper

@simple_decorator
def hello():
    print("hello world")
hello()

def complex_decorator(enter_msg,exit_msg):
    def simple_decorator(f):
        def wrapper():
            print (enter_msg)
            f()
            print (exit_msg)
        return wrapper
    return simple_decorator

@complex_decorator("func enter","func exit")
def hello():
    print("hello world")
hello()
func enter
hello world
func exit
 

posted on 2020-03-19 09:13  happygril3  阅读(79)  评论(0)    收藏  举报

导航