Pythonの坑
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の坑的更多相关文章
- 【python坑记录】
python的sort函数使用的时候有一个参数cmp.一定注意这里返回值要用1和-1.不能True和False!!!
- Python坑系列:可变对象与不可变对象
在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...
- python 坑1
目录 1.编码解码 2.基础数据类型补充: 2.1 str: 2.2list: 2.3tuple: 2.4dict: 2.5set: 3.坑 4.类型转换: 5.数据类型: 1.编码解码 编码:将文字 ...
- ArcGIS 字段计算器 Python 坑
最近要处理个简单数据,一个字段中为文本类型,包含各种描述.要求是包含平方米的数值提取出来,变成数值,如果包含多个,则把各个值累加起来. 比如 字段值为 “非法占用100平方米” 处理后结果为 100 ...
- python坑之input获取字符串
space = input("set user quotation:").strip() quotation = int(space* 1024 * 1024) print(quo ...
- IEEE Bigger系列题解
Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...
- 关于python数据序列化的那些坑
-----世界上本来没那么多坑,python更新到3以后坑就多了 无论哪一门语言开发,都离不了数据储存与解析,除了跨平台性极好的xml和json之外,python要提到的还有自身最常用pickle模块 ...
- 安装python爬虫scrapy踩过的那些坑和编程外的思考
这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时 ...
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...
随机推荐
- Linux入门基础(七):Linux软件管理基础
源代码形式 绝大多数开源软件都是直接以源代码形式发布 源代码一般会被打包成tar.gz的归档压缩文件 程序源代码需要编译成为二进制形式之后才能够运行 源代码基本编译流程 : ./configure 检 ...
- Caffe Blob针对图像数据在内存中的组织方式
Caffe使用Blob结构在CNN网络中存储.传递数据.对于批量2D图像数据,Blob的维度为 图像数量N × 通道数C × 图像高度H × 图像宽度W 显然,在此种场景下,Blob使用4维坐标定位数 ...
- 背景颜色 - bootStrap4常用CSS笔记
.bg-primary 重要的背景颜色 .bg-success 执行成功背景颜色 .bg-info 信息提示背景颜色 .bg-warning 警告背景颜色 .bg-danger 危险背景颜色 .bg- ...
- CUDA、CUDNN在Mac Book Pro上安装的问题
由于原版MacOS自带Nvidia驱动版本过低,导致最新版本CUDA安装后无法运行.具体症状为:在编译时一切正常,在运行CUDA相关程序时报错: CUDA driver version is insu ...
- Centos7 zabbix 分布式监控
分布式监控 zabbix Server ===> zabbix agent (只能同一个局域网监控) 分布式监控: a. 分担压力,降低负载 b. 多机房 ...
- comet4j推送 405/500 JSON转换异常
因为Comet4J工作在NIO方式下,所以我们需要调整服务器连接器配置,更换为NOI连接器. 打开server.xml文件将找到原先的连接器配置: <Connector executor=&qu ...
- UVALive 7464 Robots(模拟)
7464Robots Write a program to collect data from robots. We are given two sets of robotsX=fX1;:::;Xmg ...
- PSP Daily新增功能说明书
1.选择输入类别时可以记录原来的输入,支持用户选择记录清空功能 2.添加“恢复最近”button,点击这个按钮可以跳出一个页面显示最近的excel记录,用户可以通过勾选相应的excel文件名,恢复选中 ...
- 2017秋-软件工程第十二次作业(一)-PSP总结
[回顾]:回顾开学时的博客并回答相关问题 1.回想一下你曾经对计算机专业的畅想当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么?答:当初的决定是以前的事情,没有改变.经历 ...
- 用java进行简单的万年历编写
import java.util.Scanner; public class PrintCalendarDemo1 { public static void main(String[] args) { ...