当前位置:网站首页>Closure, decorator
Closure, decorator
2022-07-06 05:28:00 【Naive code writing】
Closure :
characteristic :
1. There is a function in the function
2. The inner function must use the outer function variable
3. The return value must be an inner function
Be sure to write it down
# Electronic scale
# Can calculate :
Price =( Gross weight - Packing weight )* The unit price
Packing weight :0.1 Jin
Price =( Gross weight -0.1)* The unit price
Apple :3 element / Jin
Banana :5 element / Jin
def price(weight,unitPrice):
return (weight-0.1)*unitPrice
apple=price(10.1,3)
print(apple)
banana=price(10.1,5)
print(banana)
Closure
def price(unitPrice):
def computer(weight):
return (weight-0.1)*unitPrice
return computer
apple=price(3)# Don't execute , Equivalent to return value function computer
print(apple(10.1))
banana=price(5)
print(banana(10.1))
practice :
Construct a closure program , Be able to calculate the square as needed 、 cube .
def jisuan(x):
def a(y):
return y**x
return a
pingfang=jisuan(2) #2 yes jisuan Parameter variables for
print(pingfang(5))
def jisuan(x):
def b(y):
return y**x
return b
lifang=jisuan(3)
print(lifang(9))
Decorator ( Decorators ):
( Have time to explain in detail )
Design a decorator , Able to calculate the circular area 、 Square area .
import math
def jisuan(mianji):
def kkk(*args,**kwargs):
print("==== area ====")
x=mianji(*args,**kwargs)
return x
return kkk
@jisuan
def mianji1(r):
return math.pi*r**2
@jisuan
def mianji2(d):
return d**2
print(mianji1(3))
print(mianji2(5))
Running results :
area
28.274333882308138
area
25
边栏推荐
- Hyperledger Fabric2. Some basic concepts of X (1)
- Modbus协议通信异常
- Check the useful photo lossless magnification software on Apple computer
- 03. 开发博客项目之登录
- Modbus protocol communication exception
- Promotion hung up! The leader said it wasn't my poor skills
- [leetcode16] the sum of the nearest three numbers (double pointer)
- Self built DNS server, the client opens the web page slowly, the solution
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- Questions d'examen écrit classiques du pointeur
猜你喜欢

Compilation and connection of shader in games202 webgl (learn from)

flutter 实现一个有加载动画的按钮(loadingButton)

03. 开发博客项目之登录

浅谈镜头滤镜的类型及作用

Excel转换为Lua的配置文件

Vulhub vulnerability recurrence 71_ Unomi

Vulhub vulnerability recurrence 68_ ThinkPHP

Problems encountered in installing mysql8 on MAC

Figure database ongdb release v-1.0.3

Check the useful photo lossless magnification software on Apple computer
随机推荐
Self built DNS server, the client opens the web page slowly, the solution
UCF (2022 summer team competition I)
Ora-01779: the column corresponding to the non key value saving table cannot be modified
自建DNS服务器,客户端打开网页慢,解决办法
毕业设计游戏商城
Nacos TC setup of highly available Seata (02)
Sword finger offer II 039 Maximum rectangular area of histogram
UCF (summer team competition II)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Codeforces Round #804 (Div. 2) Editorial(A-B)
01. 开发博客项目之项目介绍
C进阶-数据的存储(上)
First acquaintance with CDN
Qt TCP 分包粘包的解决方法
Hyperledger Fabric2. Some basic concepts of X (1)
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Promotion hung up! The leader said it wasn't my poor skills
Zoom and pan image in Photoshop 2022
Promise summary
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备