当前位置:网站首页>Closures and decorators
Closures and decorators
2022-07-29 01:13:00 【:D...】
Closure
Inner and outer functions are nested , The inner function refers to Not Global variables , The external function returns the object of the internal function
advantage : Renew the life of variables shortcoming : Waste of memory
# Inner and outer functions are nested
def outer(a, b):
def inner():
# Internal functions refer to non global variables under the scope of external functions
print(a + b)
# The outer function returns the inner function object
return inner
outer(5, 9)()
def f1(n):
def f2():
list1 = [i for i in range(1, n+1)]
print(list1)
return f2
f1(9)()
Decorator
Without changing the original function , Additional function extensions to existing functions .
Do not modify the existing functionSource codeandCall modeAddnew function
The difference between closures and decorators : The decorator is essentially a closure function , But when the decorator has only one parameter and is a function type , He's the decorator , Otherwise, it is closure
# Use the idea of closure to realize Calculate the program usage time print(" It's a fine day today ")
import time
def outer(f):
def inner():
start = time.time()
f()
end = time.time()
print(end - start-1)
return inner
@outer # call Decorator outer
def func(): # func Parameters passed into the decorator f
print(" It's a fine day today ")
time.sleep(1)
func() # Equate to innr()
Universal decorator
# The inner function is consistent with the decorated function { Parameters are consistent , Return consistent }
# Define a decorator , Add to the program print(" Trying to perform addition ") Program function Realize the addition of several numbers by passing parameters
def outer(f):
def inner(*args):
print(" Trying to perform addition ")
f(*args)
return inner
@outer
def f1(*args):
print(sum(args))
f1(5, 9, 9, 45, 34)
# Define a decorator Add <p> </p>
def outer(f):
def inner():
return "<p>" + f() + "</p>"
return inner
@outer
def func():
return " Life is too short , I use python"
print(func())
Multi layer decorator
# The decorator on the first floor decorates The second layer is decorated with ornaments
# The first ornament
def outer(f):
def inner():
print(" This is a decorator 1 Start of execution ")
f()
print(" This is a decorator 1 End of execution ")
return inner
# The second ornament
def outer2(f):
def inner2():
print(" This is a decorator 2 Start of execution ")
f()
print(" This is a decorator 2 End of execution ")
return inner2
@outer
@outer2
def func():
print(" Life is too short , I use python")
func()
Execution results ============>
This is a decorator 1 Start of execution
This is a decorator 2 Start of execution
Life is too short , I use python
This is a decorator 2 End of execution
This is a decorator 1 End of execution
边栏推荐
- Solid smart contract tutorial (5) -nft auction contract
- Intel带你初识视觉识别--OpenVINO
- How to carry out engineering implementation of DDD Domain Driven Design
- 时间复杂度、空间复杂度的学习总结
- 【mysql】字符串转int
- Hilbert transform and instantaneous frequency
- Univariate function integration 1__ Indefinite integral
- Hash table~
- 用CDO进行nc数据的不规则裁剪
- Seven marketing strategies of NFT project
猜你喜欢

【idea】查询字段使用位置

Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation

Summary of process and thread knowledge points 1
![[target detection] Introduction to yolor theory + practical test visdrone data set](/img/cd/3cb13d6d79cd207c6d637af7756ffc.png)
[target detection] Introduction to yolor theory + practical test visdrone data set

【Leetcode-滑动窗口问题】

18张图,直观理解神经网络、流形和拓扑

Thread lock and its ascending and descending levels

How to create a custom 404 error page in WordPress

对接支付宝支付

ACM SIGIR 2022 | 美团技术团队精选论文解读
随机推荐
Synchronized关键字详解
【刷题笔记】链表内指定区间反转
正则校验与时间格式化
System Verilog common syntax
用CDO进行nc数据的不规则裁剪
Definition of double linked list~
[target detection] Introduction to yolor theory + practical test visdrone data set
Django使用MySQL数据库已经存在的数据表方法
一元函数积分学之1__不定积分
双链表的定义 ~
[Commons lang3 topic] 003- randomstringutils topic
Visual full link log tracking
Seven SQL performance optimizations that spark 3.0 must know
How to explain JS' bind simulation implementation to your girlfriend
深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
状态压缩dp-蒙德里安的梦想
【ManageEngine】局域网监控软件是什么,有什么作用
[notes for question brushing] specified interval reversal in the linked list
[Commons lang3 topic] 002 randomutils topic
[Commons lang3 topic] 001 stringutils topic