设为首页 加入收藏

TOP

装饰器2(一)
2023-09-23 15:44:23 】 浏览:260
Tags:

装饰器

装饰器的简易版本

import time
def index():
    time.sleep(3)
    print('from index')
    
    
def home():
    print('from home')
    
def func():
    print('from func')
    
    
def outer(func_name): 
    # func_name = index 
    def get_time():
        # 1. 函数执行之前打一个时间点
        start_time = time.time()
        func_name() # index() home()


        # 2. 在函数执行之后,在打一个时间点
        end_time = time.time()

        print('总共执行了:%s' % (end_time - start_time))
	return get_time

# get_time(index)
# get_time(home)

index=outer(index)  # res=get_time
index() # get_time()

装饰器解决参数问题

import time
def index():
    time.sleep(3)
    print('from index')
    
    
def home(name, u, a):
    print('from home')
    return 'from home'
def func():
    print('from func')
    
    
def outer(func_name): 
    # func_name = index 
    def get_time(*args, **kwargs): args=() kwargs={'username':jerry, 'age':18}
        # 1. 函数执行之前打一个时间点
        
        start_time = time.time()
        func_name(*args, **kwargs) # index() home()
        res=func_name(name, username='jerry', age=18) # index() home()


        # 2. 在函数执行之后,在打一个时间点
        end_time = time.time()

        print('总共执行了:%s' % (end_time - start_time))
        return res
	return get_time

# get_time(index)
# get_time(home)

index=outer(index)  # res=get_time
index() # get_time()


home=outer(home)  # res=get_time
res=home('kevin', username='jerry', age=18) # get_time()
print(res) #  None


"""认证登录的装饰器,当你访问函数的时候,必须登录之后才能够访问!"""

装饰器的固定模板

def index():
    pass


def outer(func_name):
    def inner(*args, **kwargs):
        '''添加一些函数执行之前的功能'''
        res=func_name(*args, **kwargs) # 这个就是执行的真正的函数
        '''添加一些函数执行之后的功能'''
        return res
    
    return inner

'''装饰器本质上还是函数!'''

@outer
@outer # index=outer(index)
def index():
    pass

# index=outer(index)
index()

"""
1. 语法糖的书写规范
	@装饰器名字
	把语法糖紧贴着写在函数的头部
2. 装饰器原理:
	把被装饰对象当成函数的参数传递给装饰器的形参
"""

双层语法糖

import time


def outer(func):
    def get_time(*args, **kwargs):
        start_time = time.time()
        res = func(*args, **kwargs)  # 只能够统计index函数的时间
        end_time = time.time()
        print('执行时间:%s' % (end_time - start_time))
        return res

    return get_time

def login_auth(func):
    # func = index
    def auth():
        username = input('username:>>>').strip()
        password = input('password:>>>').strip()
        # 2. 比较用户名和密码
        if username == 'jerry' and password == '123':
            # 执行函数
            print('登录成功')
            func()
        else:
            print('用户名或者密码错误')
    return auth

@login_auth # index=login_auth(get_time) # index=auth
@outer      # get_time=outer(index)
def index():
    time.sleep(3)
    print('from index')

index() # auth()

三层语法糖(多层)

# 判断七句print执行顺序
def outter1(func1):
    print('加载了outter1')
    def wrapper1(*args, **kwargs):
        print('执行了wrapper1')
        res1 = func1(*args, **kwargs)
        return res1
    return wrapper1

def outter2(func2):
    print('加载了outter2')
    def wrapper2(*args, **kwargs):
        print('执行了wrapper2')
        res2 = func2(*args, **kwargs)
        return res2
    return wrapper2

def outter3(func3):
    print('加载了outter3')
    def wrapper3(*args, **kwargs):
        print('执行了wrapper3')
        res3 = func3(*args, **kwargs)
        return res3
    return wrapper3


@outter1
@outter2
@outter3
def index():
    print('from index')
index()

装饰器的修复技术(了解)

import time

from functools import wraps
def outer(func):
    @wraps(func) # 修复技术
    def get_time():
        start_time = time.time()
        func()  # 只能够统计index函数的时间
        end_time = time.time()
        print('执行时间:%s' % (end_time - start_time))
    return get_time

# @outer  # index=outer(index)
def index():
    print('from inde
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数据分析(以kaggle上的加州房价.. 下一篇python采集数据保存csv, 文件内容..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目