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十六坑(中文)

时间: 2024-08-22 07:55:39

Pythonの坑的相关文章

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

在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大差别.这和Python的对象机制有关.现在谈谈这个问题! 我们知道,Python有可变对象和不可变对象,他们的表现行为也迥然不同.先来几个简单的问题: 1 def foo1(arg): 2 arg = 5 3 print(arg) 4 5 x = 1 6 foo(x) # 输出5 7 print(x

Python开发过程中17个坑

一.不要使用可变对象作为函数默认值 复制代码代码如下: In [1]: def append_to_list(value, def_list=[]):   ...:         def_list.append(value)   ...:         return def_list   ...: In [2]: my_list = append_to_list(1) In [3]: my_listOut[3]: [1] In [4]: my_other_list = append_to_l

Python数组创建中的这些坑,你造吗?

本文和大家分享的主要是python 中创建数组过程中的一些坑,一起来看看吧,希望对大家 学习python 有所帮助. 1.问题的引出 在做leetcode 的题目时,遇到了一个求数组中出现次数最多的 k 个元素的题,参照答案的思路,首先利用一个 dict 记录所有元素出现的次数, key:value 中的 key 表示元素, value 表示元素出现的次数,随后根据元素出现的次数将元素放入对应的桶中,桶是一个二维数组,桶中第一个元素保存出现次数为 0 的元素,桶中第二个元素保存出现次数为 1 的

关于python+django的又一坑,请python班的同学进来看

事情是这样的. 在使用django1.6+的时候,默认会吧session存放在数据库django_session表里. 如果要把session放在内存中,就应该在settings.py 中配置 SESSION_ENGINE = "django.contrib.sessions.backends.cache" 使用的时候: 新增:request.session["userName"] = "Tom" 查询:name = request.sessio

python学习遇到的坑

1. 定义变量名和函数名或关键字重名: typeerror 在网上随便copy了一个处理csv文件的源码,运行中总是在input时出错,差错没注意变量声明,导致浪费不少时间 在红色剪头处,input的输出用input变量名存放,当再次调用input函数时,就会提示:TypeError: str object is no callable 2. python输入csv文件内容有多余空行 Python中的csv的writer,打开文件的时候,要通过binary模式去打开,即带b的,比如wb,ab+等

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

这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用.但是以前一直用的java和php,对python不熟悉,于是花一天时间粗略了解了一遍python的基础知识.然后就开干了,没想到的配置一个运行环境就花了我一天时间.下面记录下安装和配置scrapy踩过的那些坑吧. 运行环境:CentOS 6.0 虚拟机 开始上来先得安装python运行环境.然而我运行了一下python命令,发现已经自带了,窃(大)喜(坑).于是go

python 之走坑的道路

### python之 继续走函数的坑 上篇文章简单介绍了函数的一些简单的使用,这次继续踩函数的坑1.函数对象 函数其实也可以当做一个参数传给另一个函数,也可以使用赋值的方式来给另一个,而且这两个的内存地址都是一 样的 def f1(): print('这个是引用的函数测试') func = f1 print(f1) # 不加括号为foo函数的内存地址 这个其实很有用处,稍后解释 print(func) # func指向foo内存地址 func() # foo内存地址,加()执行 ####函数可

初学python 遇到的坑

这最近人工智能比较火,看了一下大多都是python的代码,最近看看python 的代码,一出来就遇到了坑,空格的问题先不说了直接上代码吧 # coding = utf-8 import urllib.request #import ssl #ssl._create_default_https_context = ssl._create_unverified_context response = urllib.request.urlopen('https://www.douban.com/') p

Python多进程相关的坑

Python的multiprocessing模块实现了多进程功能,但官方文档上只有一些比较简单的用法,主要是使用函数作为process的target,而如何在class中使用多进程并没有多讲解.google出两篇比较详细的文章,建议从它们入门: https://pymotw.com/2/multiprocessing/basics.html https://pymotw.com/2/multiprocessing/communication.html 下面记录一下自己这周在python多进程上碰