当前位置:网站首页>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
边栏推荐
- 美摄科技云剪辑,助力哔哩哔哩使用体验再升级
- AMBA协议学习小记
- Mysql database installation and configuration details
- [8] Assertion failed: dims.nbDims == 4 || dims.nbDims == 5
- 关于短视频平台框架搭建与技术选型探讨
- enforce fail at inline_container.cc:222
- PyTorch 17. GPU并发
- 【无标题】制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码
- STM32多路测温无线传输报警系统设计(工业定时测温/机舱温度定时检测等)
- unhandled system error, NCCL version 2.7.8
猜你喜欢
随机推荐
AUTOSAR从入门到精通100讲(八十六)-UDS服务基础篇之2F
PyTorch 22. PyTorch常用代码段合集
Hanlp分词器(通过spark)
《Attention in Natural Language Processing》翻译
LPDDR4笔记
PyTorch 19. Differences and relations of similar operations in pytorch
RISCV MMU 概述
AUTOSAR从入门到精通100讲(八十一)-AUTOSAR基础篇之FiM
公专融合对讲机是如何实现多模式通信下的协同工作?
机器学习——模型优化
【技术规范】:如何写好技术文档?
imx6ull-qemu 裸机教程1:GPIO,IOMUX,I2C
By onnx checker. check_ Common errors detected by model
带您遨游太空,美摄科技为航天创意小程序提供全面技术支持
美摄科技云剪辑,助力哔哩哔哩使用体验再升级
项目文件“ ”已被重命名或已不在解决方案中、未能找到与解决方案关联的源代码管理提供程序——两个工程问题
EasyUI combobox determines whether the input item exists in the drop-down list
AUTOSAR从入门到精通100讲(五十一)-AUTOSAR网络管理
【点云系列】Multi-view Neural Human Rendering (NHR)
Intuitive understanding of torch nn. Unfold