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. Go语言2

    Go语言特点: 类型检查:编译时 运行环境:编译成机器代码直接运行 编程范式:面向接口,函数式编程,并发编程 Go并发编程 采用CSP(Communication Sequenication Proc ...

  2. Python3中isdigit(), isdecimal(), isnumeric()的区别和字符串的常用方法

    # 全部小写 string.lower() # 全部大写 string.upper() # 是否全部小写 string.islower() # 是否全部大写 string.isupper() # 首字 ...

  3. 关于java调用Dll文件的异常 Native library (win32-x86-64/CtrlNPCDLL.dll) not found in resource pat

    解决办法  将dll文件放入项目bin目录下

  4. Iron Speed Designer设计工具开发总结

    9.0版本: 1.1 ISP和VS不要同时生成,代码写在override方法之下,不然生成之后会覆盖;正常情况下,ISP可以写代码,只不过没有快捷提示,一般我们先注释一下字段(如://sdsfdsfd ...

  5. CF 675E Trains and Statistic

    草稿和一些题解而已 因为指针太恶心了 所以query决定还是要试试自己yy一下 #include<cstdio> #include<cstring> #include<i ...

  6. kafka consumer demo

    kafka消费者demo pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmln ...

  7. CentOS 7 安装图形化界面及 Xshell 连接

    CentOS 比较适合用作服务器的系统,之前用过 CentOS 6,但是在配置 Nginx 的时候,发现很多语句版本7的系统都进行了更新,而且网上针对版本7的例子会更多一下,遂将系统换成版本7. 下载 ...

  8. java 实现验证码功能

    所需文件以及技术: · SecurityUtil.java   (后面我会复制给大家) · 图像处理技术 · 向客户端输出io流 一,实现的原理,当视图页面加载的时候通过<img >元素的 ...

  9. React 之容器组件和展示组件相分离解密

    Redux 的 React 绑定库包含了 容器组件和展示组件相分离 的开发思想.明智的做法是只在最顶层组件(如路由操作)里使用 Redux.其余内部组件仅仅是展示性的,所有数据都通过 props 传入 ...

  10. Babel 入门教程

    Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行. 这意味着,你可以现在就用 ES6 编写程序,而不用担心现有环境是否支持.下面是一个例子. // 转码前 inpu ...