问题描述
在 Luciano Ramalho 的 Fluent Python 中,可迭代对象被定义为一个对象,其中实现了 __iter__
方法,没有附加特征。
我目前正在为外行编写一个教程,其中我试图将 Python 的核心概念分块,以便让新手更易于管理编程。
当我将这些对象与“大小”的概念(因此也是 length
)联系起来时,我发现更容易解释可迭代对象及其对这些人的效用。通过说“可迭代对象是有长度的对象”并因此与 len
函数联系在一起,我能够自然地使用常用类型(例如标准库 list
)来演化循环和迭代的概念、dict
、tuple
、str
以及 numpy.ndarray
、pandas.Series
和 pandas.DataFrame
。
但是,由于现在我知道 __iter__
方法的唯一必要性,因此与 len
的类比可能会失败。 Ramalho 甚至在他的书中提供了一个即兴example:
import re
import reprlib
RE_WORD = re.compile(r'\w+')
class Sentence:
def __init__(self,text):
self.text = text
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
for match in RE_WORD.finditer(self.text):
yield match.group()
正如预期的那样,Sentence
的任何实例都是可迭代的(我可以使用 for
循环),但 len(Sentence('an example'))
会引发 TypeError
。
由于上述所有对象都是可迭代对象并且实现了 __len__
方法,我想知道 Python 中是否有相关对象是可迭代对象 (__iter__
),但没有长度 (__len__
) 所以如果我能确定是在我的教程中添加脚注还是找出不同的类比。
解决方法
文件没有长度:
>>> with open("test") as f:
... print(len(f))
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
TypeError: object of type '_io.TextIOWrapper' has no len()
像在 open 中那样遍历文件会遍历行,即由换行符分隔的文本块。要知道有多少行,必须完整读取该文件,然后进行迭代 - 根据文件的大小,这可能需要很长时间,否则计算机可能会耗尽 RAM。
,迭代器是无处不在的迭代器,通常不提供长度:
>>> len(iter('foo'))
Traceback (most recent call last):
File "<pyshell#1>",in <module>
len(iter('foo'))
TypeError: object of type 'str_iterator' has no len()
>>> len(iter((1,2,3)))
Traceback (most recent call last):
File "<pyshell#3>",in <module>
len(iter((1,3)))
TypeError: object of type 'tuple_iterator' has no len()
>>> len(iter([1,3]))
Traceback (most recent call last):
File "<pyshell#0>",in <module>
len(iter([1,3]))
TypeError: object of type 'list_iterator' has no len()