当前位置:网站首页>PyTorch 13. 嵌套函数和闭包(狗头)
PyTorch 13. 嵌套函数和闭包(狗头)
2022-04-23 06:11:00 【DCGJ666】
嵌套函数
python允许在函数里定义函数,而且现有的作用域和变量生存周期依旧不变。
def A():
name='mysql'
def A_children(): #outer函数内部定义的函数
print name
return A_children() #返回该内部函数
A()
理解:
在A的嵌套函数中,python解释器需要找一个叫name的本地变量,查找失败后会继续在上层的作用域(即,外部函数中)寻找。
对于A函数中最后一句,返回子函数调用的结果,需要知道一点是,子函数仅仅是一个遵循python变量解析规则的变量名,python解释器会优先在A的作用域里面对变量名A_children查找匹配变量。
把恰好是函数标识符的变量A_children作为返回值返回回来,每次函数outer被调用的时候,函数A_children都会被重新定义,如果它不被当做变量返回的话,每次执行过后它将不复存在。
在python里,函数就是对象,它也只是一些普通的值,也就是说你可以把函数像参数一样传递给其他的函数或者说从函数里面返回函数。
return内层函数时不加括号,只返回函数的地址
也就是说如果return A_children,则不会执行子函数
函数作为变量
# 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函数准备接收一个函数的变量,它也只是一个普通的变量而已,和其他变量一样。然后我们调用传进来的函数:“()代表调用的操作,并且调用变量包含的值”
闭包
# encoding=utf-8
def outer():
name = "python"
def inner():
print name
return inner
res = outer() #第一个括号,得到inner的地址
res() #第二个括号调用函数inner()
print res.func_closure #打印闭包里包含哪些外部变量
通过上面变量的作用域和生存周期,不难明白,name是函数outer里的一个局部变量,也就是说只有当outer正在运行时,该变量才会存在。
根据python的运行模式,我们没法在函数outer执行退出之后还能继续调用inner函数,之所以能够调用成功,因为python支持一个叫函数闭包的特性。
闭包定义:如果一个函数定义在另一个函数的作用域内,并且引用了外层函数的变量,则该函数称为闭包。闭包是python所支持的一种特性,它让非global scope定义的函数可以引用其外围空间中的变量,这些外围空间中被引用的变量叫做这个函数的环境变量。环境变量和这个非全局函数一起构成了闭包。
闭包特点:一个函数返回的函数对象,这个函数对象执行的话依赖非函数内部的变量只,这个时候,函数返回的实际内容如下:
- 函数对象
- 函数对象需要使用的外部变量和变量值
通俗理解就是:里面函数执行,需要用到外面函数的一个变量,所以,就把外面变量和里面这个函数合到一块,和到一块的这两个东西就是闭包。
版权声明
本文为[DCGJ666]所创,转载请带上原文链接,感谢
https://blog.csdn.net/DCGJ666/article/details/121694786
边栏推荐
- Android interview Online Economic encyclopedia [constantly updating...]
- Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
- 【3D形状重建系列】Implicit Functions in Feature Space for 3D Shape Reconstruction and Completion
- Exploration of SendMessage principle of advanced handler
- 读书小记——Activity
- [point cloud series] a rotation invariant framework for deep point cloud analysis
- Project, how to package
- 1.1 PyTorch和神经网络
- [dynamic programming] different paths 2
- Data class of kotlin journey
猜你喜欢
【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling
Cancel remote dependency and use local dependency
【点云系列】Relationship-based Point Cloud Completion
WebView displays a blank due to a certificate problem
第2章 Pytorch基础1
Miscellaneous learning
【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds
MySQL installation and configuration - detailed tutorial
[2021 book recommendation] effortless app development with Oracle visual builder
机器学习笔记 一:学习思路
随机推荐
[dynamic programming] triangle minimum path sum
Gephi教程【1】安装
face_recognition人脸检测
Fill the network gap
Chapter 3 pytoch neural network toolbox
Use originpro express for free
【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
1.2 preliminary pytorch neural network
xcode 编译速度慢的解决办法
Component learning (2) arouter principle learning
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
Android interview Online Economic encyclopedia [constantly updating...]
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
【2021年新书推荐】Learn WinUI 3.0
如何对多维矩阵进行标准化(基于numpy)
Minesweeping games
5种方法获取Torch网络模型参数量计算量等信息
Compression and acceleration technology of deep learning model (I): parameter pruning
【点云系列】Fully-Convolutional geometric features
给女朋友写个微信双开小工具