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. elk6.3 centos集群搭建 head插件安装

    版本elk均为6.3+centos7.0 准备工作 官网下载elk6.3的linux环境的压缩包,sftp上传 下载对应的head插件sftp上传到指定目录 tar.gz文件解压 tar -zxvf ...

  2. Docker创建容器

    容器是镜像的一个运行实例,是基于镜像运行的轻量级环境,是一个或者一组应用. 怎样创建容器?将容器所基于的镜像名称传入即可,Docker会从本地仓库中寻找该镜像,如果本地仓库没有,则会自动从远程仓库中拉 ...

  3. Spring AOP 报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXXXXX' defined in class path resource..........

    完整报错如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'befo ...

  4. 3星|《规避政治风险:全球化企业必修课》:中国将赢得5G竞争

    规避政治风险:全球化企业必修课(<哈佛商业评论>增刊) <哈佛商业评论>的两篇文章+<财经>的1篇文章.把<财经>的文章放到增刊中,好像是第一次,我觉得 ...

  5. vue 动画

    Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.包括以下几种常见的方式: 在 CSS 过渡和动画中自动应用 class 可以配合使用第三方 CSS 动画库,如 Animate ...

  6. Netty源码分析第3章(客户端接入流程)---->第2节: 处理接入事件之handle的创建

    Netty源码分析第三章: 客户端接入流程 第二节: 处理接入事件之handle的创建 上一小节我们剖析完成了与channel绑定的ChannelConfig初始化相关的流程, 这一小节继续剖析客户端 ...

  7. 如何配置php客户端(phpredis)并连接Redis--华为DCS for Redis使用经验系列

    使用php连接Redis.Memcache等都需要进行扩展,以CentOS为例,介绍phpredis的客户端环境搭建. 第0步:准备工作 华为云上购买1台弹性云服务器ECS(我选了CentOS 6.3 ...

  8. Python函数初识二

    一.变量的作用域LEGB 1.1.变量的作用域 在Python中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名 ...

  9. js中if else switch 条件判断的替代方法

    function condition(test){ return({ cat :function(){console.log('cat');}, dog :function(){console.log ...

  10. Scrum立会报告+燃尽图(Final阶段第二次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...