当前位置:网站首页>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
边栏推荐
- 【刷题笔记】链表内指定区间反转
- How to create a custom 404 error page in WordPress
- New pseudo personal guide page source code
- RHCE命令练习(二)
- 【AD学习】本次海上航行器大赛画pcb图的历程
- 【刷题笔记】二进制链表转整数
- What opportunities does the London gold real-time market bring?
- [Commons lang3 topic] 005- objectutils topic
- Beginner's Guide to electronic bidding
- Necessary interview skills for Android (including interview questions and learning materials)
猜你喜欢

图扑软件亮相 2022 福州数博会,携手共创数字新时代

Plato launched the LAAS protocol elephant swap, which allows users to earn premium income

Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network

进程和线程知识点总结 2

可视化全链路日志追踪

Hilbert transform and instantaneous frequency

ThinkPHP high imitation blue cloud disk system program

How to create a custom 404 error page in WordPress

浅谈一下跨端技术方案

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
随机推荐
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
Naver 三方登录
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
[Commons lang3 topic] 004- numberutils topic
Visual full link log tracking
(perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
Hilbert transform and instantaneous frequency
【unity】将unity编辑c#配置为vscode
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
mysql分表之后怎么平滑上线?
Necessary interview skills for Android (including interview questions and learning materials)
正则校验与时间格式化
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
How to check the redis Version (check the redis process)
[AD learning] the course of PCB drawing in this marine vehicle competition
Introduction to FLV documents
How to create a custom 404 error page in WordPress
Brushless DC motor controller (how much is the hall charge for changing the motor)
递归与分治
🧐 table1 | 一秒搞定你的三线表