当前位置:网站首页>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
边栏推荐
- 【动态规划】三角形最小路径和
- 【2021年新书推荐】Learn WinUI 3.0
- [recommendation of new books in 2021] enterprise application development with C 9 and NET 5
- ThreadLocal,看我就够了!
- 【2021年新书推荐】Practical IoT Hacking
- Chapter 3 pytoch neural network toolbox
- [point cloud series] pnp-3d: a plug and play for 3D point clouds
- 【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
- MySQL的安装与配置——详细教程
- 【点云系列】Unsupervised Multi-Task Feature Learning on Point Clouds
猜你喜欢
Cancel remote dependency and use local dependency
Chapter 5 fundamentals of machine learning
【点云系列】 A Rotation-Invariant Framework for Deep Point Cloud Analysis
[point cloud series] sg-gan: advantageous self attention GCN for point cloud topological parts generation
【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
Bottom navigation bar based on bottomnavigationview
Gee configuring local development environment
【指标】Precision、Recall
Component based learning (1) idea and Implementation
Project, how to package
随机推荐
【动态规划】杨辉三角
How to standardize multidimensional matrix (based on numpy)
Handlerthread principle and practical application
[2021 book recommendation] practical node red programming
【点云系列】Learning Representations and Generative Models for 3D pointclouds
【 planification dynamique】 différentes voies 2
Mysql database installation and configuration details
[dynamic programming] longest increasing subsequence
Chapter 3 pytoch neural network toolbox
树莓派:双色LED灯实验
Computer shutdown program
[2021 book recommendation] artistic intelligence for IOT Cookbook
第1章 NumPy基础
Easyui combobox 判断输入项是否存在于下拉列表中
【指标】Precision、Recall
Pytorch best practices and coding style guide
PyTorch 模型剪枝实例教程三、多参数与全局剪枝
.net加载字体时遇到 Failed to decode downloaded font:
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
免费使用OriginPro学习版