据我了解,我可以for
在对象上使用循环构造和__iter__
返回迭代器的方法。我有一个对象,我为此实现了以下__getattribute__
方法:
def __getattribute__(self,name):
if name in ["read","readlines","readline","seek","__iter__","closed","fileno","flush","mode","tell","truncate","write","writelines","xreadlines"]:
return getattr(self.file,name)
return object.__getattribute__(self,name)
我有一个此类的对象,a
发生以下情况:
>>> hasattr(a,"__iter__")
True
>>> for l in a: print l
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'TmpFile' object is not iterable
>>> for l in a.file: print l
...
>>>
所以python看到它a
有一个__iter__
方法,但不认为它是可迭代的。我做错了什么?这是python 2.6.4。