Python Closures are these inner functions that are enclosed within the outer function. Closures can access variables present in the outer function scope. It can access these variables even after the outer function has completed its execution.
Python闭包是那些封闭在外部函数中的内部函数。闭包可以访问存在于外部函数作用域中的变量。即使在外部函数完成执行之后,它也可以访问这些变量。

python 嵌套函数中的作用域是如何工作的

def outer(name):# 外部函数(enclosing function)def inner():# 内部函数(enclosed function)# 内部函数访问外部函数 name 变量print(name)inner()# 调用外部函数
outer('Soapcmd')
# Output:
# Soapcmd# 即: 闭包可以访问存在于外部函数作用域中的变量

python 闭包

The inner function becomes a closure when we return the inner function instead of calling it
返回内部函数而不是直接执行它,则内部函数就会成为一个闭包

So we have a closure in Python if

We have a nested function, i.e. function within a function
The nested function refers to a variable of the outer function
The enclosing function returns the enclosed function
闭包的三个条件:
嵌套函数
内部函数访问外部函数的变量
外部函数返回内部函数

def outer(name):# 外部函数(enclosing function)def inner():# 内部函数(enclosed function)# 内部函数访问外部函数 name 变量print(name)return inner # 这里直接返回而非调用# 调用外部函数
myFunction = outer('Soapcmd') # 外部函数已执行完毕
myFunction() # name变量依然被记住
# Output:
# Soapcmd# 即 在外部函数完成执行之后,我们也可以访问这些变量

应用

  1. To replace the unnecessary use of class: Suppose you have a class that contains just one method besides the init method. In such cases, it is often more elegant to use a closure instead of a class.
    如果一个class除__init__之外只有一个方法
  2. To avoid the use of the global scope: If you have global variables which only one function in your program will use, think closure. Define the variables in the outer function and use them in the inner function.
    如果有个全部变量只在一个函数里使用
  3. To implement data hiding: The only way to access the enclosed function is by calling the enclosing function. There is no way to access the inner function directly.
    隐藏内部函数
  4. To remember a function environment even after it completes its execution: You can then access the variables of this environment later in your program.
    获得一个函数执行完成后的变量

Python 闭包 Closures相关推荐

  1. 深入理解Python闭包概念

    闭包并不只是一个Python中的概念,在函数式编程语言中应用较为广泛.理解python中的闭包一方面是能够正确的使用闭包,另一方面可以好好体会和思考闭包的设计思想. 1.概念介绍 首先看一下维基上对闭 ...

  2. python 闭包理解与应用

    一.概念介绍 闭包并不只是一个python中的概念,在函数式编程语言中应用较为广泛.理解python中的闭包一方面是能够正确的使用闭包,另一方面可以好好体会和思考闭包的设计思想. 首先看一下维基上对闭 ...

  3. Python闭包及其作用域

    Python闭包及其作用域 关于Python作用域的知识在python作用域有相应的笔记,这个笔记是关于Python闭包及其作用域的详细的笔记 如果在一个内部函数里,对一个外部作用域(但不是全局作用域 ...

  4. Python 闭包详解

    在函数编程中经常用到闭包.闭包是什么,它是怎么产生的及用来解决什么问题呢.给出字面的定义先:闭包是由函数及其相关的引用环境组合而成的实体(即:闭包=函数+引用环境)(想想Erlang的外层函数传入一个 ...

  5. Python闭包与延迟绑定

    Python闭包 1.什么是闭包,闭包必须满足以下3个条件: 必须有一个内嵌函数 内嵌函数必须应用外部函数的变量 外部函数的返回值必须是内嵌函数 举个栗子 # 嵌套函数但不是闭包 def nested ...

  6. python闭包的延迟绑定_浅析Python的闭包和延迟绑定

    def multipliers(): return [lambda x : i * x for i in range(4)] print [m(2) for m in multipliers()] 你 ...

  7. Python闭包与装饰器

    Python闭包与装饰器 一.闭包       函数是一个对象,所以可以对象的形式作为某个函数的结果返回.函数执行完后内部变量将会被回收.在闭包中,由于内部函数存在对外部函数的变量的引用,所以即使外部 ...

  8. python 闭包和装饰器

    python 闭包和装饰器 一.闭包 闭包:外部函数FunOut()里面包含一个内部函数FunIn(),并且外部函数返回内部函数的对象FunIn,内部函数存在对外部函数的变量的引用.那么这个内部函数F ...

  9. python闭包的应用场景_Python闭包函数定义与用法分析

    python函数的闭包怎么理解小编憎恨离别.但若,离别能让你牵挂,小编愿意离开你. 1. 闭包的概念 首先还得从基本概念说起,什么是闭包呢?来看下维基上的解释: 复制代码代码如下: 在计算机科学中,闭 ...

最新文章

  1. 还在用Logback?Log4j2的异步性能已经无敌了,还不快试试
  2. double类型数值计算出现误差的解决办法
  3. agc015D A or...or B Problem
  4. LiveVideoStack线上交流分享 ( 六 ) —— VP9 Scalable Video Coding
  5. java list 去重复元素_java List去掉重复元素的几种方式
  6. 工作总结8:关于Vue中的slot-scope=“scope“
  7. 请问如何让Dundas chart for asp.net控件产生的临时图片文件减少一些?
  8. 为什么总是有人说Java啰嗦,却没人说C++啰嗦?
  9. [BZOJ1135][P3488][POI2009]LYZ[线段树+Hall定理]
  10. 安装redis3.2.4集群时出现的一些坑
  11. JSPatch库, 一个Apple官方支持的实现在线更新iOS应用的库
  12. mysql的驱动connect放在哪里_MySQL的JDBC驱动程序下载地址
  13. fgui的ui管理框架_ET框架FGUIxasset的梦幻联动
  14. matlab泊松分布图,matlab画泊松分布图
  15. 项目管理到底是一个什么样的职位,具体都做些什么事情?
  16. java excel 冻结_Java 冻结或解除冻结Excel中的行和列的方法
  17. STM32F303+LAN9252的EtherCAT实现IO、AD、DA
  18. C# ABP WebApi与Swagger UI的集成
  19. 赵小楼《天道》《遥远的救世主》深度解析(9)肖亚文的“小心思和小算计”
  20. Automated defect inspection system for metal surfaces based on deep learning and data augmentation

热门文章

  1. cudnn网站打不开
  2. primitive原始数据类型
  3. VNPY官方开源项目为什么不能选择MIT开源协议
  4. mac 管理员变成普通用户,没有全名,也无法解锁问题的解决方法
  5. 输入正整数,显示所有小于或等于该数的素数
  6. 关于CSGO理财游戏的一些心得(一)
  7. 【百金轻】:乍暖还寒 最难将息
  8. openKylin社区首届咨询委员会会议成功召开!
  9. matlab 输入普朗克常量,利用matlab和excel进行光电效应测普朗克常量实验中的数据处理...
  10. spyder恢复原始的页面结构及字体调整