当前位置:网站首页>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
边栏推荐
- Using stopwatch to count code time
- Unity Vector3. Use and calculation principle of reflect
- Figure database ongdb release v-1.0.3
- Select knowledge points of structure
- UCF (2022 summer team competition I)
- 【torch】|torch.nn.utils.clip_grad_norm_
- 03. Login of development blog project
- Please wait while Jenkins is getting ready to work
- Vulhub vulnerability recurrence 69_ Tiki Wiki
- HAC cluster modifying administrator user password
猜你喜欢
flutter 实现一个有加载动画的按钮(loadingButton)
pix2pix:使用条件对抗网络的图像到图像转换
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
用StopWatch 统计代码耗时
图数据库ONgDB Release v-1.0.3
Sword finger offer II 039 Maximum rectangular area of histogram
Modbus协议通信异常
[effective Objective-C] - memory management
[cloud native] 3.1 kubernetes platform installation kubespher
Codeforces Round #804 (Div. 2) Editorial(A-B)
随机推荐
UCF(暑期团队赛二)
【华为机试真题详解】检查是否存在满足条件的数字组合
SQLite add index
[leetcode16] the sum of the nearest three numbers (double pointer)
MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
js Array 列表 实战使用总结
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
CUDA11.1在线安装
MySQL if and ifnull use
Three.js学习-光照和阴影(了解向)
Some common skills on unity inspector are generally used for editor extension or others
04. 项目博客之日志
Questions d'examen écrit classiques du pointeur
Problems encountered in installing mysql8 on MAC
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
LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
February 12 relativelayout
nacos-高可用seata之TC搭建(02)
2022 half year summary
EditorUtility. The role and application of setdirty in untiy