装饰器

Posted on 2018-03-16 00:08  Brown羊羊  阅读(94)  评论(0编辑  收藏  举报

本质是函数,装饰其他函数,就是为其他函数添加附加功能
 

原则:

1.不能修改被装饰的函数的源代码
2. 不能修改被装饰的函数的调用方式
 

实现装饰器知识储备:

1.函数即“变量”
2.高阶函数
   a.把一个函数名当做一个实参传给另外一个函数(不修改被装饰函数源代码情况下为其添加新功能)
   b.返回值中包含函数名(不修改函数的调用方式)
3.嵌套函数 
 
举个例子什么叫高阶函数:
描述a
def bar():
print("in the bar")

def test(func):
print("in the foo")
func()
print("end")

test(bar)
 
描述b
# -*- coding:utf-8 -*- 
# Author:Brownyangyang
def bar():
print("in the bar")

def test(func):
print(func)
return func

#把return的返回值传给bar
bar=test(bar) ##这里传的是bar不能是bar(),bar是函数,bar()就是bar函数的运行结果
bar()


举个例子什么叫嵌套函数:
# -*- coding:utf-8 -*- 
# Author:Brownyangyang
def foo():
print("in the foo")

def bar():
print("in the bar")

bar()

foo()

举个装饰器的例子:
# -*- coding:utf-8 -*- 
# Author:Brownyangyang
import time
def timer(func):  ##timer(test1)  func=test1
    def deco():
        start_time = time.time()
        func()   ##执行test1
        stop_time = time.time()
        print("the func run time")
    return deco

def test1():
    time.sleep(3)
    print('in the test1')

print(timer(test1))  ##打印deco的内存地址
test1 = timer(test1) ##把deco内存地址给test1
test1()   ##现在执行test1就是执行deco,这里的test1现在已经不是原来定义的test1了,原来的test1是func

 

上面等同于:

# -*- coding:utf-8 -*-
# Author:Brownyangyang
import time
def timer(func):  ##timer(test1)  func=test1
    def deco():
        start_time = time.time()
        func()   ##执行test1
        stop_time = time.time()
        print("the func run time")
    return deco

@timer
def test1():
    time.sleep(3)
    print('in the test1')

print(timer(test1))  ##打印deco的内存地址
test1()   ##现在执行test1就是执行deco,这里的test1现在已经不是原来定义的test1了,原来的test1是func

 

如果test1()要传参数呢

# -*- coding:utf-8 -*-
# Author:Brownyangyang
import time
def timer(func):  ##timer(test1)  func=test1
    def deco(*args,**kwargs):
        start_time = time.time()
        func(*args,**kwargs)   ##执行test1
        stop_time = time.time()
        print("the func run time")
    return deco

@timer
def test1(name,age):
    time.sleep(3)
    print('in the test1',name,age)

print(timer(test1))  ##打印deco的内存地址
test1("liyang",22)   ##现在执行test1就是执行deco,这里的test1现在已经不是原来定义的test1了,原来的test1是func

 

如果我想要test1参数有返回值呢

# -*- coding:utf-8 -*-
# Author:Brownyangyang
import time
def timer(func):  ##timer(test1)  func=test1
    def deco(*args,**kwargs):
        start_time = time.time()
        res = func(*args,**kwargs)   ##执行test1
        stop_time = time.time()
        print("the func run time")
        return res
    return deco

@timer
def test1(name,age):
    time.sleep(3)
    print('in the test1',name,age)
    return "from test1"

print(timer(test1))  ##打印deco的内存地址
test1("liyang",22)
print(test1("liyang",22))
解释下:在执行test1的时候,res = func(*args,**kwargs)的意思是执行了func函数并且把返回值"from test1" 赋值给了res


# -*- coding:utf-8 -*-
# Author:Brownyangyang

user_status=False
def login(func):
def inner(*args,**kwargs):

_username="liyang"
_password="123456"
global user_status

if user_status==False:
username=input("请输入用户名:")
password=input("请输入密码:")
if username==_username and password==_password:
print("验证通过")
user_status=True
else:
print("用户名或者密码错误")

if user_status==True:
func(*args,**kwargs)
return inner

@login
def america(lanmu):
#login()
print("hello,I am amrica",lanmu)

america("AV")