7.5这个数字有什么特殊含义吗?

2026-04-11 09:291阅读0评论SEO基础
  • 内容介绍
  • 相关推荐

本文共计919个文字,预计阅读时间需要4分钟。

7.5这个数字有什么特殊含义吗?

今日学习内容:封闭函数的简介

1.定义:在函数内部的函数,外部无法直接访问。

2.特征:

- 1. 定义在函数内部的函数 - 2. 使用了外部函数的命名空间中的变量 - 3. 外部函数名空间中的名字必须符合命名规则

7.5这个数字有什么特殊含义吗?

今日学习内容
  • 闭包函数的简介

1.定义在函数内部的函数 2.内部函数使用了外部函数名称空间中的名字 必须符合上述两个特征的的函数才能称之为闭包函数 def func(): username = 'joker' def index(): print(username) return index res = func() print(res) res() 闭包函数的实际应用 def index(username): def func(): print(username) return func res = index('joker') res() # joker res1 = index('jason') res1() # jason

  • 装饰器的简介
    装饰器并不是一个全新的知识点 而是我们早两天学习函数知识整合到一起的产物

1.装饰器的本质 是在不改变装饰对象原来的调用方式和内部代码的情况下给被装饰对象添加新的功能 2.装饰器的原则 对修改封闭 对扩展开放 3.储备知识 import time print(time.time()) # 1657020096.241627 '''时间戳(秒数):当前距离1970年1月1日0时0分0秒所经历的秒数''' 实际应用>>>:统计代码的运行时间 import time print(time.time()) start_time = time.time() for i in range(1000): print(i) end_time = time.time() print('for循环执行的时间:%s'%(end_time - start_time)) # 0.01900482177734375 time.sleep(5) # 让程序原地等待五秒

  • 装饰器推导流程

统计index函数的执行时间 import time def index(): time.sleep(3) print('form func') start_time = time.time() index() end_time = time.time() print(end_time - start_time) # 3.005446434020996 '''有缺陷:如果有多个index需要统计时间 则需要重复的编写代码 解决措施可以将它封装成函数 ''' def get_time(): start_time = time.time() index() end_time = time.time() print(end_time - start_time) get_time() # 3.0104970932006836 '''虽然说这样能运行起来但函数体代码写死了,如果有多个函数体代码需要统计时间,上述的解决措施不够完善 解决措施:给函数体添加形参(动态绑定) ''' def home(): time.sleep(3) print('from home') def get_time(xxx): start_time = time.time() xxx() end_time = time.time() print(end_time - start_time) get_time(index) # 3.0094032287597656 get_time(home) # 3.0089964866638184 '''缺陷3:不同形参个数的函数无法兼容统计 解决措施*args,**kwargs 但是目前还无法实现'''

  • 装饰器针对有参无参函数如何兼容

import time def outer(xxx): def get_time(*args,**kwargs): start_time = time.time() # 调用函数index函数之前获取一下时间戳 xxx(*args, **kwargs) end_time = time.time() print(end_time - start_time) return get_time def index(name): time.sleep(3) print('form index') def home(): time.sleep(3) print('from home') home = outer(home) home() index = outer(index) index('jason') import time 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 def home(): time.sleep(3) print('from home') return '执行home的返回值' def index(name): time.sleep(3) print('form index') return '执行index的返回值' home = outer(home) xxx = home() print(xxx) index = outer(index) res = index('joker') print(res)

  • 装饰器的固定模板

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返回值' '''装饰器语法糖''' 如果想给函数装上装饰器 在函数上面加一个@outer import time @outer def home(): time.sleep(1) print('from home') return 'home返回值'

本文共计919个文字,预计阅读时间需要4分钟。

7.5这个数字有什么特殊含义吗?

今日学习内容:封闭函数的简介

1.定义:在函数内部的函数,外部无法直接访问。

2.特征:

- 1. 定义在函数内部的函数 - 2. 使用了外部函数的命名空间中的变量 - 3. 外部函数名空间中的名字必须符合命名规则

7.5这个数字有什么特殊含义吗?

今日学习内容
  • 闭包函数的简介

1.定义在函数内部的函数 2.内部函数使用了外部函数名称空间中的名字 必须符合上述两个特征的的函数才能称之为闭包函数 def func(): username = 'joker' def index(): print(username) return index res = func() print(res) res() 闭包函数的实际应用 def index(username): def func(): print(username) return func res = index('joker') res() # joker res1 = index('jason') res1() # jason

  • 装饰器的简介
    装饰器并不是一个全新的知识点 而是我们早两天学习函数知识整合到一起的产物

1.装饰器的本质 是在不改变装饰对象原来的调用方式和内部代码的情况下给被装饰对象添加新的功能 2.装饰器的原则 对修改封闭 对扩展开放 3.储备知识 import time print(time.time()) # 1657020096.241627 '''时间戳(秒数):当前距离1970年1月1日0时0分0秒所经历的秒数''' 实际应用>>>:统计代码的运行时间 import time print(time.time()) start_time = time.time() for i in range(1000): print(i) end_time = time.time() print('for循环执行的时间:%s'%(end_time - start_time)) # 0.01900482177734375 time.sleep(5) # 让程序原地等待五秒

  • 装饰器推导流程

统计index函数的执行时间 import time def index(): time.sleep(3) print('form func') start_time = time.time() index() end_time = time.time() print(end_time - start_time) # 3.005446434020996 '''有缺陷:如果有多个index需要统计时间 则需要重复的编写代码 解决措施可以将它封装成函数 ''' def get_time(): start_time = time.time() index() end_time = time.time() print(end_time - start_time) get_time() # 3.0104970932006836 '''虽然说这样能运行起来但函数体代码写死了,如果有多个函数体代码需要统计时间,上述的解决措施不够完善 解决措施:给函数体添加形参(动态绑定) ''' def home(): time.sleep(3) print('from home') def get_time(xxx): start_time = time.time() xxx() end_time = time.time() print(end_time - start_time) get_time(index) # 3.0094032287597656 get_time(home) # 3.0089964866638184 '''缺陷3:不同形参个数的函数无法兼容统计 解决措施*args,**kwargs 但是目前还无法实现'''

  • 装饰器针对有参无参函数如何兼容

import time def outer(xxx): def get_time(*args,**kwargs): start_time = time.time() # 调用函数index函数之前获取一下时间戳 xxx(*args, **kwargs) end_time = time.time() print(end_time - start_time) return get_time def index(name): time.sleep(3) print('form index') def home(): time.sleep(3) print('from home') home = outer(home) home() index = outer(index) index('jason') import time 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 def home(): time.sleep(3) print('from home') return '执行home的返回值' def index(name): time.sleep(3) print('form index') return '执行index的返回值' home = outer(home) xxx = home() print(xxx) index = outer(index) res = index('joker') print(res)

  • 装饰器的固定模板

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返回值' '''装饰器语法糖''' 如果想给函数装上装饰器 在函数上面加一个@outer import time @outer def home(): time.sleep(1) print('from home') return 'home返回值'