装饰器

定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
原则:
1.不能修改被装饰的函数的源代码
2.不能修改被装饰的函数的调用方法

 

实现装饰器知识储备
1.函数即“变量”
2.高阶函数 

  a.把一个函数名当做实参传给另外一个函数
  b.返回值中包含函数名(不修改函数的调用方式)

3.嵌套函数
高阶函数+嵌套函数==>装饰器

 

例子:

 

import time
def timer(func):
    def warpper():
        start_time = time.time()
        func()
        stop_time = time.time()
        print("the fun time is %s"%(stop_time - start_time))
    return  warpper
@timer  #相当于 test1 = warpper(test1)
def test1():
    time.sleep(1)
    print("sleep in test1")

test1()
装饰器基础版

 

import time
def timer(func):
    def warpper(*args,**kwargs):#*args,**kwargs可以通用于有不同参数传递
        start_time = time.time()
        func(*args,**kwargs)#*args,**kwargs可以通用于有不同参数传递
        stop_time = time.time()
        print("the fun time is %s"%(stop_time - start_time))
    return  warpper
@timer  #相当于 test1 = warpper(test1)
def test1():
    time.sleep(1)
    print("sleep in test1")
@timer
def test2(name):#因为这里有传递的参数,原来的装饰器不适用,必须增加对应的参数,而为了适应不同的参数传递,所以上方使用args,**kwargs
    time.sleep(1)
    print("name:",name)

test1()
test2("GaShuaiGe")
装饰器中级版

 带return后,会将函数的结果也返回

import  time
def decorator(func):
    def warper(*args,**kwargs):
        start_time = time.time()
        res = func()
        stop_time = time.time()
        print("in the warper func time is %s"%(stop_time-start_time))
        return res #这个是用来返回函数结果,若没有这个home()里面return "from home"没有
    return  warper


def index():
    print("this is index page")
@decorator
def home():
    time.sleep(1)
    print("this is home page")
    return "from home"
@decorator
def bbs():
    time.sleep(1)
    print("this is bbs page")

index()
print("index()",index())
home()
print("home()",home())
bbs()
print("bbs()",bbs())
带return的装饰器中级版

 高级版:再嵌套一层

import  time
user , passwd = "alex","123"
def decorator(decorator_type):#将local以及ssh传入
    #print(decorator_type)
    def outwarper(func):
        def warper(*args, **kwargs):
            print("*args, **kwargs",*args, **kwargs)
            if decorator_type == "local":
                username = input("username:").strip()
                password = input("password:").strip()
                if username == user and passwd == password:
                    res = func(*args, **kwargs)
                    print("\033[31;1mwelcome!\033[0m")
                    return res
                else:
                    print("--------------------------")
                    exit("\033[33;1m用户名密码错误!\033[0m")
            elif decorator_type =="ssh":
                print("----------------")
                exit("\033[33;1m没有ssh!\033[0m")

        return warper
    return outwarper

def index():
    print("this is index page")
@decorator(decorator_type = "local")#decorator_type = "local"指定不同的方式
def home():
    time.sleep(1)
    print("this is home page")
    return "from home"

@decorator(decorator_type = "ssh")
def bbs():
    time.sleep(1)
    print("this is bbs page")


index()
#print("index()",index())
home()
#print("home()",home())
bbs()
#print("bbs()",bbs())
装饰器高级版

 

posted @ 2017-12-19 20:24  雨之愿风  阅读(133)  评论(0编辑  收藏  举报