% python
Python 3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
... def __iter__(self):
... return []
...
>>> iter(Foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: iter() returned non-iterator of type 'list'
而documentation doesn't make it very clear,这是因为
__iter__
必须(而不是应该)返回一个迭代器,而不是iterable:相关问题 更多 >
编程相关推荐