装饰器

装饰器前言

  装饰器本质是一个函数,为其他函数增加其功能的

  装饰器的原则:
    1,不该变原来的调用方式,

    2,不该表原代码

  实现装饰器知识储备:
    1,函数即  ‘变量’   

        (和变量一样需要先定义,后调用)(pthon 的回收机制是什么?引用机制,当函数,

           或者变量没有引时(del删除变量  名,)内部会有一个定时器,定时的清理没有变量引用的内存地址)

        (大楼门牌号(变量),内容(函数体))

    2,高阶函数

        1,把一个函数名当做实参传给另一个函数(在不修改被装饰函数的原代码为其添加其功能)

        2,返回值中包含函数名(不修改函数的调用方式)

    3,嵌套函数 

  装饰器  = 高级函数 + 嵌套函数 

装饰器演示 

import time
def timmer(fun):
    def warpper(*args,**kwargs)
        start_time = time.time()
        fun()
        ent_time = time.time()
        print(ent_time-start_time)
    return warrper

@timmer
def text():
    time.sleep(3)
    print('in the text')

text()

用高阶函数写装饰器

 

# 高阶函数把一个函数传个另一个函数。

只是用高阶函数写的,(没有用嵌套函数所以还不完整,现在只是像了一点),还差一步。
import time def bar(): time.sleep(2) print('in the bar') def test1(func): print('添加的内容') return func # test1(bar) #这里改变了调用方式。 # bar() bar = test1(bar) bar() #写成这样  

函数嵌套   +   高阶函数   =  装饰器

  

import time
def timer(func): #timer(test1)  func=test1
    def deco(*args,**kwargs):   # 函数的嵌套
        start_time=time.time()
        func(*args,**kwargs)   #run test1()
        stop_time = time.time()
        print("the func run time  is %s" %(stop_time-start_time))
    return deco       # 返回内容存地址,
@timer  #test1=timer(test1)
def test1():
    time.sleep(1)
    print('in the test1')
@timer # test2 = timer(test2)  = deco  test2(name) =deco(name)
def test2(name,age):
    print("test2:",name,age)
test1()
test2("alex",22)

上面的装饰器满足我们日常所用到百分之80以上功能(下面是更牛的)

 

import time
user,passwd = 'alex','abc123'
def auth(auth_type):
    print("auth func:",auth_type)
    def outer_wrapper(func):
        def wrapper(*args, **kwargs):
            print("wrapper func args:", *args, **kwargs)
            if auth_type == "local":
                username = input("Username:").strip()
                password = input("Password:").strip()
                if user == username and passwd == password:
                    print("33[32;1mUser has passed authentication33[0m")
                    res = func(*args, **kwargs)  # from home
                    print("---after authenticaion ")
                    return res
                else:
                    exit("33[31;1mInvalid username or password33[0m")
            elif auth_type == "ldap":
                print("搞毛线ldap,不会。。。。")

        return wrapper
    return outer_wrapper

def index():
    print("welcome to index page")
@auth(auth_type="local") # home = wrapper()
def home():
    print("welcome to home  page")
    return "from home"

@auth(auth_type="ldap")
def bbs():
    print("welcome to bbs  page")

index()
print(home()) #wrapper()
bbs()

  

  

原文地址:https://www.cnblogs.com/littlesky1124/p/9471940.html