【发布时间】:2013-12-19 13:45:40
【问题描述】:
我正在使用生成器,并且我有以下代码。我对它的逻辑有点困惑。
只要最后有一个“False”,函数中的生成器就可以正常工作。删除它会在运行函数时导致StopIteration 异常。
谁能在这里解释一下False的作用?
>>> def some(coll, pred= lambda x:x)
... return next((True for item in coll if pred(item)),False)
...
>>> some([0,'',False])
False
>>> def some(coll, pred= lambda x:x):
... return next((True for item in coll if pred(item)))
...
>>> some([0,'',False])
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
some([0,'',False])
File "<pyshell#63>", line 2, in some
return next((True for item in coll if pred(item)))
StopIteration
【问题讨论】:
-
The documentation 很清楚这一点......
标签: python python-3.x iterator generator