ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

闭包函数与装饰器

2022-07-05 22:01:14  阅读:120  来源: 互联网

标签:闭包 index 函数 time print home 装饰 def


闭包函数简介

闭包函数
	1、定义在函数内部的函数
    2、内部函数使用了外部函数名称空间中的名字
ps:只有符合上述两个特征的函数才能称之为闭包函数

def func(username):
    # username = 'curry'
    def index():
        print(username)
    return index
# 思考如何在全局调用index
# res = func()
# print(res)
# res()

func('curry')  #  username = 'curry'

闭包函数实际应用

1、给函数体胆码传值得方式:通过形参
def func(xxx):
	print(xxx)
2、给函数体代码传值得方式:闭包函数
def index(username):
    # username = 'curry'
    def func():
        print(username)
    return func
res = index('curry')
res()  # curry
res()   # curry
res()  # curry
res1 = index('jason')
res1()  # jason
res1()  # jason
res1()  # jason

装饰器简介

1、装饰器的本质
	在不改变或装饰对象原来的“调用方式”和“内部代码”的情况下给装饰对象添加新的功能
    eg:
        def func():
            print(123)
        func()  # 每次执行之前需要校验用户身份
2、装饰器的原则
	对修改封闭 对扩展开放
    import time
3.知识储备
print(time.time())  # 1657017466.4727514
'''时间戳(秒数):当前距离1970年1月1日0时0分0秒所经历的秒数'''
# 实际应用>>>:统计代码的运行时间
# start_time = time.time()
# for i in range(1000):
#     print(i)
# end_time = time.time()
# print('for循环的执行时间是:%s'%(end_time-stat_time))
time.sleep(3)
'''让程序原地等待三秒'''
print('小张睡不醒')
装饰器前期推导
import time

def index():
    time.sleep(3)
    print('from index')
'''统计index函数的执行时间'''
start_time = time.time()  # 在调用index函数之前获取一下时间戳
index()  # 调用index函数
end_time = time.time()
print(end_time - start_time)
"""
缺陷
    如果有多个index需要统计时间 则需要重复编写代码
    解决措施:
        封装成函数
"""
def get_time():
    start_time = time.time()  # 在调用index函数之前获取一下时间戳
    index()     # 调用index函数
    end_time = time.time()
    print('函数的执行时间:end_time - start_time')
"""
缺陷
    不用形参个数的函数无法兼容统计
    解决措施:
    *args  **kwargs
    但是在我们目前的代码中无法实现(暂且忽略)
"""
def funcl(a):
    time.sleep(1)
    print('from func1')
"""
缺陷
    改变了原来的调用方式
    解决措施:
        装饰器推导流程
"""

装饰器各种版本

import time

"""针对有参无参函数如何兼容"""
def outer(xxx):
    def get_time(*args,**kwargs):
        start_time = time.time()  # 在调用index函数之前获取时间戳
        res = xxx(*args,**kwargs)  # 调用index函数
        end_time = time.time()
        print('函数的执行时间是:',end_time - start_time)
        return res
    return get_time()

def home():
    time.sleep(2)
    print('from home')
    return '执行home函数之后的返回值'

def index(name):
    time.sleep(1)
    print('from index')
    return '执行index函数之后的返回值'

home = outer(home)
xxx = home()
print(xxx)

index = outer(index)
res = index('jason')
print(res)

home = outer(home)
home()

def func(a,b,c):
    time.sleep(1)
    print('from func')

index = outer(index)
index('jason')

home = outer(home)
home()

func = outer(func)
func(1,2,3)
装饰器的固定模板
from functools import wraps
def outer(func_name):
    @wraps(func_name)   # 仅仅是为了让装饰器不容易被别人发现 做到真正的以假乱真
    def inner(*args,**kwargs):
        print('执行被装饰对象之前可以做的额外操作')
        res = func_name(*args,**kwargs)
        print('执行被装饰对象之后可以做的额外操作')
        return res
    return inner
import time
def home()
    time,sleep(1)
    print('from home')
    return 'home返回值'

home = outer(home)
res = home()
print(res)
"""
执行home函数之前需要添加校验用户身份的功能
"""

装饰器语法糖
import time

@outer   #  home = outer(真正的函数名home)
def home():
    '''我是home函数 天气很热'''
    time.sleep(1)
    print('from home')
    return 'home返回值'

help(home)
print(home)
home()

def index():
    '我是index函数 我的功能很强大'
    pass

help(index)

标签:闭包,index,函数,time,print,home,装饰,def
来源: https://www.cnblogs.com/zzjjpp/p/16448785.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有