Python closures and late binding

A closure occurs when a function has access to a local variable from an enclosing scope that has finished its execution.

def make_printer(msg):
def printer():
print msg
return printer

We can see that the printer() function depends on the variable msg which is defined outside the scope of it’s function.

Late binding and bad side-effects

Python’s closures are late binding. This means that the values of variables used in closures are looked up at the time the inner function is called.

def multipliers():
return [lambda x : i*x for i in range(4)] print [m(2) for m in multipliers()] # [6, 6, 6, 6]

Then we expect the output of the print statement to be [0, 2, 4, 6] based on the element-wise operation [0*2, 1*2, 2*2, 3*2]. However, [3*2, 3*2, 3*2, 3*2] = [6, 6, 6, 6] is what is actually return. That is because i is not passed to the the lambda function until the loop for i in range(4) has been evaluated.

def multipliers():
return [lambda x, i=i : i * x for i in range(4)] print [m(2) for m in multipliers()] # [0, 2, 4, 6]

附:

python十坑(英文)

python十六坑(中文)

python的global和nonlocal

Pythonの坑的更多相关文章

  1. 【python坑记录】

    python的sort函数使用的时候有一个参数cmp.一定注意这里返回值要用1和-1.不能True和False!!!

  2. Python坑系列:可变对象与不可变对象

    在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...

  3. python 坑1

    目录 1.编码解码 2.基础数据类型补充: 2.1 str: 2.2list: 2.3tuple: 2.4dict: 2.5set: 3.坑 4.类型转换: 5.数据类型: 1.编码解码 编码:将文字 ...

  4. ArcGIS 字段计算器 Python 坑

    最近要处理个简单数据,一个字段中为文本类型,包含各种描述.要求是包含平方米的数值提取出来,变成数值,如果包含多个,则把各个值累加起来. 比如 字段值为 “非法占用100平方米” 处理后结果为 100 ...

  5. python坑之input获取字符串

    space = input("set user quotation:").strip() quotation = int(space* 1024 * 1024) print(quo ...

  6. IEEE Bigger系列题解

    Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...

  7. 关于python数据序列化的那些坑

    -----世界上本来没那么多坑,python更新到3以后坑就多了 无论哪一门语言开发,都离不了数据储存与解析,除了跨平台性极好的xml和json之外,python要提到的还有自身最常用pickle模块 ...

  8. 安装python爬虫scrapy踩过的那些坑和编程外的思考

    这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...

  9. Python开发者须知 —— Bottle框架常见的几个坑

    Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...

随机推荐

  1. 微信小程序日记(一)

    一.基础知识(目录与配置) (1)标签 小程序的view相当于HTML的div标签一样,作占位 (2)每一个页面都需要在app.json里面注册,例如: { { "pages": ...

  2. Python 的AES加密与解密-需要安装的模块

    踩雷1: #先导入所需要的包 pip3 install Crypto #再安装pycrtpto pin3 install pycrypto from Crypto.Cipher import AES ...

  3. react.js插件开发,x-dailog弹窗浮层组件

    react.js插件开发,x-dailog弹窗浮层组件 我认为,每一个组件都应该有他自带的样式和属性事件回调配置.所以我会给x-dialog默认一套简单的样式,和各种默认的配置项.所有react插件示 ...

  4. 《Postgre SQL 即学即用 (第三版)》 分享 pdf下载

    链接:https://pan.baidu.com/s/1akR33VqEkt99UqJUfiy2OA提取码:3p1k

  5. https、ssl、tls协议学习

    一.知识准备 1.ssl协议:通过认证.数字签名确保完整性:使用加密确保私密性:确保客户端和服务器之间的通讯安全 2.tls协议:在SSL的基础上新增了诸多的功能,它们之间协议工作方式一样 3.htt ...

  6. day23 正则,re模块

    一. 简谈正则表达式 元字符 . 除了换行符外任意字符. \w 数字.字母.下划线 \s 空白符 \b 单词的末尾 \d 数字 \n 匹配换行符 \t 匹配制表符 \W 除了数字. 字母 下划线 \D ...

  7. RBC:Echo设备2020年可为亚马逊贡献100亿美元收入

    BI 中文站 12 月 22 日报道 加拿大皇家银行资本市场(RBC Capital Markets)分析师马克-马哈尼(Mark Mahaney)表示,亚马逊是首批将智能音箱引进主流受众的公司之一, ...

  8. MegaCli64/MegaCli命令详解

    基础命令学习目录首页 MegaCli64 -LDInfo -Lall -aALL这个命令能看到RAID的状态MegaCli64 -LDSetProp ForcedWB -L0 -a0MegaCli64 ...

  9. 深入理解JavaScript函数参数

    前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传进来的参数是什么数据类型,甚至可以不传参数. arguments javascri ...

  10. 实验3 --俄罗斯方块 with 20135335郝爽

    一.   实验内容 (一)敏捷开发与XP 内容:1.敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的开发方法. 2.极限编程(eXtreme Programming,X ...