当前位置:网站首页>PyTorch 13. Nested functions and closures (dog head)
PyTorch 13. Nested functions and closures (dog head)
2022-04-23 07:28:00 【DCGJ666】
PyTorch 13. Nested functions and closures ( dog's head )
Nested function
python Allows you to define functions in functions , And the existing scope and variable life cycle remain unchanged .
def A():
name='mysql'
def A_children(): #outer Functions defined inside functions
print name
return A_children() # Returns the inner function
A()
understand :
stay A In nested functions ,python The interpreter needs to find one called name Local variables for , After the search fails, it will continue to be in the upper scope ( namely , In an external function ) seek .
about A The last sentence in the function , Return the result of the sub function call , One thing to know is , A subfunction is just a function that follows python Variable name of variable resolution rule ,python The interpreter will take precedence over the A Variable names in the scope of A_children Find matching variables .
Put the variable that happens to be the function identifier A_children Return as a return value , Each function outer When called , function A_children Will be redefined , If it's not returned as a variable , It will not exist after each execution .
stay python in , Functions are objects , It's just some ordinary values , In other words, you can pass functions to other functions like parameters or return functions from functions .
return Inner function without parentheses , Only return the address of the function
That is to say if return A_children, The sub function... Will not be executed
Function as variable
# encoding=utf-8
def add(x,y):
return x+y
def sub(x,y):
return x-y
def apply(func,x,y):
return func(x,y)
print("apply(add,2,1):",apple(add,2,1))
print("apple(sub,2,1):",apple(sub,2,1))
apply The function is ready to receive a function variable , It's just an ordinary variable , Like other variables . Then we call the function passed in :“() Represents the called operation , And call the value contained in the variable ”
Closure
# encoding=utf-8
def outer():
name = "python"
def inner():
print name
return inner
res = outer() # The first bracket , obtain inner The address of
res() # The second bracket calls the function inner()
print res.func_closure # Print which external variables are included in the closure
Through the scope and life cycle of the above variables , It's not hard to understand ,name Is the function outer A local variable in , In other words, only when outer Running time , This variable will exist .
according to python The mode of operation of , We can't be in a function outer After exiting, you can continue to call inner function , The reason why it can be called successfully , because python Support a feature called function closure .
Closure definition : If a function is defined within the scope of another function , And references variables of outer functions , Then the function is called a closure . Closure is python A feature supported , It makes non global scope A defined function can reference variables in its peripheral space , The variables referenced in these peripheral spaces are called the environment variables of this function . The environment variable and this non global function together form a closure .
Closure characteristics : Function object returned by a function , The execution of this function object depends on variables that are not inside the function, and only , This is the time , The actual contents returned by the function are as follows :
- Function object
- The external variables and variable values that the function object needs to use
Popular understanding is that : Inside, the function executes , You need to use a variable of the external function , therefore , Just combine the outer variable with the inner function , The two things that come together are closures .
版权声明
本文为[DCGJ666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230611343858.html
边栏推荐
- torch.where能否传递梯度
- Systrace parsing
- Proteus 8.10安装问题(亲测稳定不闪退!)
- [8] Assertion failed: dims. nbDims == 4 || dims. nbDims == 5
- 使用proteus仿真STM32超声波SRF04测距!Code+Proteus
- GIS实用小技巧(三)-CASS怎么添加图例?
- AUTOSAR从入门到精通100讲(五十)-AUTOSAR 内存管理系列- ECU 抽象层和 MCAL 层
- 【无标题】制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码
- AUTOSAR从入门到精通100讲(八十六)-UDS服务基础篇之2F
- 江宁医院DMR系统解决方案
猜你喜欢
机器学习——PCA与LDA
GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法
基于51单片机的体脂检测系统设计(51+oled+hx711+us100)
Chapter 1 numpy Foundation
使用proteus仿真STM32超声波SRF04测距!Code+Proteus
公专融合对讲机是如何实现多模式通信下的协同工作?
Chapter 8 generative deep learning
pth 转 onnx 时出现的 gather、unsqueeze 等算子
【点云系列】SO-Net:Self-Organizing Network for Point Cloud Analysis
ARMCC/GCC下的stack protector
随机推荐
unhandled system error, NCCL version 2.7.8
Systrace 解析
GIS实战应用案例100篇(五十二)-ArcGIS中用栅格裁剪栅格,如何保持行列数量一致并且对齐?
F. The wonderful use of pad
如何利用qemu搭建SOC protoype:80行代码实现一个Cortex M4 模拟器
PyTorch 20. PyTorch技巧(持续更新)
Draw margin curve in arcface
ARMCC/GCC下的stack protector
By onnx checker. check_ Common errors detected by model
【无标题】制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码
[8] Assertion failed: dims. nbDims == 4 || dims. nbDims == 5
PyTorch 12. hook的用法
枫桥学院开元名庭酒店DMR系统解决方案
传输层重要知识(面试,复试,期末)
Unwind 栈回溯详解
【点云系列】DeepMapping: Unsupervised Map Estimation From Multiple Point Clouds
How keras saves and loads the keras model
imx6ull-qemu 裸机教程2:USDHC SD卡
机器学习——朴素贝叶斯
GIS实用小技巧(三)-CASS怎么添加图例?