在其中使用print()调用生成器上的any()会产生意外行为

时间:2017-10-04 20:45:42

标签: python generator

any一旦在任何迭代中找到一个单独的值(无论是迭代器,生成器,列表等),都应该短路。

对于普通发电机,这是真的:

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32  
Type "help", "copyright", "credits" or "license" for more information.
>>> gen = (i for i in range(100))
>>> gen
<generator object <genexpr> at 0x0341A360>
>>> next(gen)
0                         
>>> next(gen)
1   
>>> any(gen)
True                                                                                       
>>> next(gen)
3                                      
>>>

如果您在其中创建包含print的生成,any不会短路:

>>> gen = (print(i) for i in range(100))
>>> next(gen)
0
>>> next(gen)
1
>>> any(gen)
2
3
4
5
6
7
8
9
...

为什么添加print会阻止any短路?

1 个答案:

答案 0 :(得分:3)

函数print()返回None。考虑一下:

>>> gen = (print(i) for i in range(10))
>>> list(gen)
0
1
2
3
4
5
6
7
8
9
[None, None, None, None, None, None, None, None, None, None]

您可以看到生成器为每次迭代返回None

any()一直运行,直到找到真值。由于None永远不是真的,any()会一直运行完毕。

您可以通过确保生成器返回每次迭代的预期值来挽救原始代码:

>>> gen = (print(i) or i for i in range(10))
>>> next(gen)
0
0
>>> next(gen)
1
1
>>> any(gen)
2
True