当前位置:网站首页>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
边栏推荐
- xcode 编译速度慢的解决办法
- ArcGIS license server administrator cannot start the workaround
- c语言编写一个猜数字游戏编写
- 【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
- torch_ Geometric learning 1, messagepassing
- torch.mm() torch.sparse.mm() torch.bmm() torch.mul() torch.matmul()的区别
- Component learning (2) arouter principle learning
- Google AdMob advertising learning
- C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
- 微信小程序 使用wxml2canvas插件生成图片部分问题记录
猜你喜欢
![[3D shape reconstruction series] implicit functions in feature space for 3D shape reconstruction and completion](/img/4d/6d5821759766a6bf1d77ad51b69e24.png)
[3D shape reconstruction series] implicit functions in feature space for 3D shape reconstruction and completion

【2021年新书推荐】Practical Node-RED Programming

Visual Studio 2019安装与使用

【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide

【点云系列】Multi-view Neural Human Rendering (NHR)
树莓派:双色LED灯实验

【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling

【点云系列】Fully-Convolutional geometric features

1.1 PyTorch和神经网络

Binder mechanism principle
随机推荐
Chapter 3 pytoch neural network toolbox
Pymysql connection database
给女朋友写个微信双开小工具
最简单完整的libwebsockets的例子
Android interview Online Economic encyclopedia [constantly updating...]
Bottom navigation bar based on bottomnavigationview
WebView displays a blank due to a certificate problem
Machine learning III: classification prediction based on logistic regression
【3D形状重建系列】Implicit Functions in Feature Space for 3D Shape Reconstruction and Completion
Google AdMob advertising learning
[recommendation for new books in 2021] professional azure SQL managed database administration
HandlerThread原理和实际应用
Visual Studio 2019安装与使用
Record WebView shows another empty pit
Miscellaneous learning
[dynamic programming] longest increasing subsequence
Chapter 4 pytoch data processing toolbox
Fill the network gap
Five methods are used to obtain the parameters and calculation of torch network model
MySQL notes 3_ Restraint_ Primary key constraint