• 简单正常python例子:

    def up(text):
    return text.upper() #转成大写
    def lo(text):
    return text.lower() ##转成小写
    def greet(func):
    gt = func("AbCd")
    print (gt)
    greet(up)
    greet(lo)
  • 简单的装饰器:

    import time
    def c_time(func):
    def inner(*args, **kwargs):
        begin = time.time()
        func(*args, **kwargs)
        end = time.time()
        print("程序运行实际总时间:", end-begin)
    return inner
    @c_time #fac = c_time(fac)
    def fac(name):
    time.sleep(3)
    print(name + '睡了3秒')
    fac("张三")
  • 简单带参数的装饰器:
    import time
    def out(type): ##相当于套多一层函数
    def c_time(func):
        def inner(*args,**kwargs):
            begin = time.time()
            func(*args,**kwargs)
            end = time.time()
            if type==2:
               print("这是类型2,带参数的")
            print("程序运行实际总时间:", end-begin)
        return inner   ##每层都需要返回函数
    return c_time ##每层都需要返回函数
    @out(type=2)
    def fac(name):
    time.sleep(3)
    print(name + '睡了3秒')
    fac("张三")