学习python第十三天,函数5 装饰器

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

实现装饰器知识储备
1函数即变量
2、高阶函数,满足2个条件之一 1、把一个函数名当做实参传给另外一个函数。2、返回值中包含函数名
(1、在不修改被装饰的函数的源代码的情况下,为其添加功能 2、不能修改函数的调用方式)
3、嵌套函数
高阶函数+嵌套函数=》装饰器
python内存回收机制,匿名函授和变量名没有的情况下会被回收

def foo():
    print('in the foo')
    bar()
def bar():
    print('in the bar')
foo()
import time

def bar():
    time.sleep(0.5)
    print('in the bar')


def test1(func):
    start_time=time.time()
    print(func)
    time.sleep(0.5)
    func()
    stop_time=time.time()
    print("the func run time is %s"%(stop_time-start_time))


test1(bar)#打印内存地址,调用方式改变了


import time

def bar():#定义原始函数
    time.sleep(0.5)#停止0.5秒
    print('in the bar')#定义过程
def test2(func):#定义装饰器函数
    print(func)#定义过程
    return func#返回值“func”
print(test2(bar))#打印函数test2在bar中的内存地址
bar=test2(bar)#重新定义变量bar=函数test2以bar为实参
bar()#run bar#运行bar

  

嵌套函数:在一个函数的体内用def去声明一个函数

def foo():
    print('in the foo')
    def bar():#在一个函数的体内用def去声明一个函数
        print('in the bar')

    bar()
foo()
x=0
def gramdpa():
    x=1
    def dad():
        x=2
        def son():
            x=3
            print(x)
        son()
    dad()
gramdpa()

import time

  


装饰器1——>无参数装饰器(包含高阶\嵌套函数) 要实现的功能是查看“源代码”程序运行了多少时间
def timer(func):#定义一个名为timer的函数,参数为func,timer(test1)把test1的内存地址传给了func
    def deco():#声明一个新的函数deco,函数的嵌套,他的作用
        start_time=time.time()#开始时间
        func()#等于运行run test1  适用无参数的源代码
        stop_time=time.time()#结束时间
        print('the func run time is %s'%(stop_time-start_time))#打印...
    return deco#高阶函数返回值中有函数deco内存地址

@timer
#源代码
def test1(): #定义名为test1函数
    time.sleep(1)#等3秒
    print('in the test1')#函数过程 打印...
print(timer(test1))
test1=timer(test1)
test1()#--->执行deco

  

装饰器2-->含多个参数(包含高阶\嵌套函数) 要实现的功能是查看“源代码”程序运行了多少时间
import time
def timer(func):#定义一个名为timer的函数,参数为func,timer(test1)把test1的内存地址传给了func
    def deco(*args,**kwargs):#声明一个新的函数deco,函数的嵌套,他的作用
        start_time=time.time()#开始时间
        func(*args,**kwargs)#等于run test1  *args,**kwargs适用任何参数的源代码
        stop_time=time.time()#结束时间
        print('the func run time is %s'%(stop_time-start_time))#打印...
    return deco#高阶函数返回值中有函数deco内存地址

@timer
源代码
def test1(): #定义名为test1函数
    time.sleep(1)#等3秒
    print('in the test1')#函数过程 打印...
print(timer(test1))
test1=timer(test1)
test1()#--->执行deco


@timer
def test2(name,age):#定义名为test2函数,含有name参数
    print("test2:",name,age)#打印 ...

test1()
test2("dream",22)#对应源代码加入参数才能运行

  

装饰器3--输入用户名密码,(源代码没有返回数据的装饰器)

import time
user,passwd='dream','133456'#默认密码
def auth(func):
    def wrapper(*args,**kwargs):
        print("wrapper func args:",*args,**kwargs)
        username=input("Username:").strip()#移除空格和回车字符
        password=input("Password:").strip()
        if user==username and passwd==password:#验证用户名和密码
            print('\033[32;1mUser has passed authentication\033[0m')
            func(*args,**kwargs)#执行源代码
        else:
                exit('\33[31;1mInvalid username or password\033[0m')#否则退出
    return wrapper

@auth
def index():
    print('welcome to index page')
@auth
def home():
    print("welcome to home page")

@auth
def bbs():
    print("welcome to home page")

# index()
# home()
# bbs()

  



#装饰器4--输入用户名密码,源代码含有返回数据的装饰器

import time
user,passwd='dream','133456'#默认密码
def auth(func):
    def wrapper(*args,**kwargs):
#        print("wrapper func args:",*args,**kwargs)
        username=input("Username:").strip()#移除空格和回车字符
        password=input("Password:").strip()
        if user==username and passwd==password:#验证用户名和密码
            print('\033[32;1mUser has passed authentication\033[0m')
            res=func(*args,**kwargs)#定义一个变量
            return res

        else:
                exit('\33[31;1mInvalid username or password\033[0m')#否则退出
    return wrapper

@auth
def index():
    print('welcome to index page')
@auth
def home():
    print("welcome to home page")
    return "from home"

@auth
def bbs():
    print("welcome to home page")

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

  

装饰器5--输入用户名密码,判断本地认证和远程认证

import time
user,passwd='dream','133456'#默认密码
def auth(auth_type):
    print("auth func:",auth_type)
    def outer_wrapper(func):
        def wrapper(*args,**kwargs):#定义wrapper函数带有两个函数
            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('\033[32;1mUser has passed authentication\033[0m')
                    res=func(*args,**kwargs)#执行源代码
                    print('---after authenticaion')
                    return res
                else:
                    exit('\33[31;1mInvalid username or password\033[0m')#否则退出

            elif auth_type=="ldap":
                print("行不通")
        return wrapper

    return outer_wrapper

def index():
    print('welcome to index page')
@auth(auth_type="local")#本地认证
def home():
    print("welcome to home page")
    return "from home"
@auth(auth_type="ldap")      #远程认证
def bbs():
    print("welcome to home page")
#
#index()
home()
bbs()

  

 


猜你喜欢

转载自www.cnblogs.com/buildydream/p/10242875.html