首页 > 其他分享 >装饰器推导

装饰器推导

时间:2022-10-11 21:55:56浏览次数:33  
标签:index func1 推导 get time print 装饰 def

import  time
def index():
    time.sleep(3)
    print('from index')
def home():
    time.sleep(1)
    print('from home')

'''1.直接在调用index函数的前后添加代码'''
start_time = time.time()
index()
end_time = time.time()
print('函数index的执行时间为>>>', end_time - start_time)

image

import time
def index():
    time.sleep(3)
    print('from index')
def home():
    time.sleep(1)
    print('from home')

'''2.index调用的地方较多 代码不可能反复拷贝>>>:相同的代码需要在不同的位置反复执行>>>:函数'''
def get_time():
    start_time = time.time()
    index()
    end_time = time.time()
    print('函数index的执行时间为>>>:', end_time - start_time)
get_time()

image

import time
def index():
    time.sleep(3)
    print('from index')
def home():
    time.sleep(1)
    print('from home')


'''3.函数体代码写死了 只能统计index的执行时间 如何才能做到统计更多的函数运行时间 直接传参变换统计的函数'''

def get_time(xxx):   #xxx为形参,接收函数名get_time实参传进来的函数
    start_time = time.time()
    xxx()  # 接收index函数名并执行index()函数体内的代码
    end_time = time.time()
    print('函数的执行时间为>>>:', end_time - start_time)
get_time(index)  # 接收index函数名并执行get_time函数体内代码
get_time(home)   # 接收home函数名并执行get_time函数体内代码

image

import  time
def index():
    time.sleep(3)
    print('from index')
def home():
    time.sleep(1)
    print('from home')

'''4.虽然实现了一定的兼容性 但是并不符合装饰器的特征 第一种传参不写 只能考虑闭包'''
def outer(xxx):
    # xxx = index
    def get_time():
        start_time = time.time()
        index()
        end_time = time.time()
        print('函数的执行时间为>>>:', end_time - start_time)
    return get_time()
res = outer(index)
res()
res1 = outer(home)
res1()

image

import  time
def index():
    time.sleep(3)
    print('from index')
def home():
    time.sleep(1)
    print('from home')

'''5.调用方式还是不对 如何变形>>>:变量名赋值绑定(******)'''
def outer(xxx):   # xxx真正的index函数(指向index函数名绑定的内存地址)
    def get_time():
        start_time = time.time()
        xxx()
        end_time = time.time()
        print('函数的执行时间为>>>', end_time - start_time)
    return get_time
# res = outer(index)  #赋值符号的左边是一个变量名 可以随意包括命名
# res()
# res1 = outer(home)
# res1()
index = outer(index) #全局名称空间中的index不再执行原来的index函数而是get_time函数
index()
home=outer(home)   # outer(真正的index函数)
home()

image

import time
'''6.上述装饰器只能装饰无参函数 兼容性太差'''
def func(a):  # 全局名称空间中的func变量名绑定的是func的函数体代码
    time.sleep(0.1)
    print('from func', a)

def func1(a,b):
    time.sleep(0.2)
    print('from func1', a, b)

def func2():
    time.sleep(0.3)
    print('from func2')

def outer(xxx):
    def get_time(a, b):
        start_time = time.time()
        xxx(a, b)
        end_time =time.time()
        print('函数的执行时间为>>>:',end_time - start_time)
    return get_time
func1 = outer(func1) #让outer函数的形参xxx绑定func1的函数体代码
#让全局名称空间中的func变量名绑定get_time函数体代码
func1(1, 2)

image

import time
'''7.被装饰的函数不知道有没有参数及有几个参数 如何兼容'''
def func(a):
    time.sleep(0.1)
    print('from func', a)

def func1(a,b):
    time.sleep(0.2)
    print('from func1', a, b)

def outer(xxx):
    def get_time(*args, **kwargs):  # get_time(1,2,3)  args=(1,2,3)
        start_time = time.time()
        xxx(*args, **kwargs)   # xxx(*(1,2,3))   xxx(1,2,3)
        end_time =time.time()
        print('函数的执行时间为>>>:',end_time - start_time)
    return get_time
func = outer(func)
func(123)
func1=outer(func1)
func1(1,2)
import time
'''8. 如果被装饰的函数有返回值'''
def func(a):
    time.sleep(0.1)
    print('from func', a)
    return 'func'

def func1(a,b):
    time.sleep(0.2)
    print('from func1', a, b)
    return 'func1'

def outer(xxx):
    def get_time(*args, **kwargs):  # 接收形参
        start_time = time.time()
        res = xxx(*args, **kwargs)  #接收实参
        end_time =time.time()
        print('函数的执行时间为>>>:',end_time - start_time)
        return res
    return get_time
# func = outer(func)
# res = func(123)
# print(res)

func1 = outer(func1)  # 接收func1函数名,并进入到outer函数体内执行代码,并把return 的get_time函数返回并赋值给func1变量名,func1相当于就是get_time函数名。
res = func1(123, 123) # func1(即get_time函数)接收两个实参123,123并把实参带进get_time函数内,从上往下执行,
# 遇到xxx即就是第一行func1函数并去func1执行函数并把123,123带进func1函数体内,最后返回func1返回值,res就接收到func1返回值(func1),最后返回res(func1返回值)返回值。
print(res)  # 打印res返回值,即func1的返回值

标签:index,func1,推导,get,time,print,装饰,def
From: https://www.cnblogs.com/wxlxl/p/16782733.html

相关文章

  • 闭包函数与装饰器
    昨日内容回顾函数的参数位置参数 分为位置形参和位置实参,位置形参与实参数量要一致,使用关键字传参可改变赋值顺序。默认参数 函数定义阶段赋有默认值的参数,传参时可不......
  • 函数之装饰器
    global与nonlocal1.局部名称空间直接修改全局名称空间中的数据money=123defindex():globalmoneymoney=222index()print(money)2.内层局部名称空间修......
  • 函数(三)——闭包函数与装饰器
    一、函数名的多种用法函数名其实绑定的是一块内存地址,只不过该地址里面存放的不是数据值而是一段代码,函数名加括号就会找到该代码并执行。1. 可以当做变量名赋值defi......
  • 函数剩余内容/函数装饰器
    目录今日内容概要1.global与nonlocal用法2.函数名的多种用法3.闭包函数4.装饰器介绍5.装饰器原理推导过程6.装饰器模板7.装饰器语法糖练习题及答案今日内容概要global和......
  • 闭包函数与装饰器
    今日内容总结global与nonlocalmoney=666defindex():globalmoneymoney=123index()print(money)"""局部名称空间直接修改全局空间中的数据"""......
  • 函数装饰器
    目录内容概要内容详解global与nonlocal函数名的多种用法闭包函数装饰器简历装饰器推导流程装饰器模板装饰器语法糖内容概要global与nonlocal函数名的多种用法闭包......
  • 函数与装饰器
    目录global与nonlocal函数名的多种用法函数闭包装饰器简介装饰器推导流程装饰器模板装饰器语法糖作业global与nonlocal1global关键字:在函数中,如果想给全局变量赋值,则需......
  • 函数对象与闭包函数与装饰器
    目录一.global与nonlocal二.函数名的多种用法1.可以当做变量名赋值2.可以当做函数的参数3.可以当做函数的返回值4.可以当做容器类型的数据三.闭包函数四.装饰器1.装饰器简......
  • python基础之闭包函数与装饰器
    python基础之闭包函数与装饰器目录一、global与nonlocal二、函数名的多种用法1.可以当变量名2.可以当函数的参数3.可以当函数的返回值三、闭包函数1.闭包函数的实际应用四......
  • 装饰器
    1.global与nonlocal1.global:局部名称空间修改全局名称空间的数据n=100defindex():globalnn=999index()print(n)#9992.nonlocal:内层局部名......