当前位置:网站首页>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
边栏推荐
- 1.2 初试PyTorch神经网络
- PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
- 【点云系列】Relationship-based Point Cloud Completion
- Android暴露组件——被忽略的组件安全
- 常见的正则表达式
- 【動態規劃】不同路徑2
- C connection of new world Internet of things cloud platform (simple understanding version)
- 机器学习 三: 基于逻辑回归的分类预测
- torch.mm() torch.sparse.mm() torch.bmm() torch.mul() torch.matmul()的区别
- “Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
猜你喜欢

机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类

【2021年新书推荐】Professional Azure SQL Managed Database Administration

c语言编写一个猜数字游戏编写

C connection of new world Internet of things cloud platform (simple understanding version)

微信小程序 使用wxml2canvas插件生成图片部分问题记录
![[point cloud series] pnp-3d: a plug and play for 3D point clouds](/img/83/3662bc668602110236e43ee0b9d7ac.png)
[point cloud series] pnp-3d: a plug and play for 3D point clouds

ThreadLocal, just look at me!

C language, a number guessing game

【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation

1.1 PyTorch和神经网络
随机推荐
Android interview Online Economic encyclopedia [constantly updating...]
Record WebView shows another empty pit
What did you do during the internship
Easyui combobox 判断输入项是否存在于下拉列表中
Minesweeping games
【点云系列】点云隐式表达相关论文概要
【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide
【2021年新书推荐】Professional Azure SQL Managed Database Administration
WebRTC ICE candidate里面的raddr和rport表示什么?
Chapter 1 numpy Foundation
HandlerThread原理和实际应用
Binder mechanism principle
Handler进阶之sendMessage原理探索
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
Use originpro express for free
torch_ Geometric learning 1, messagepassing
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
Cancel remote dependency and use local dependency
ThreadLocal,看我就够了!