【发布时间】:2011-08-24 09:06:15
【问题描述】:
我刚刚开始学习 python 并且正在阅读有关 classes 的内容。
这是我为一个简单的可迭代类编写的代码:
class maths:
def __init__(self,x):
self.a=x
def __iter__(self):
self.b=0
return self
def next(self):
if self.b <= self.a:
self.b = self.b+1
return self.b-1
else:
raise StopIteration
x=maths(5)
for l in x:
print l
当我使用__next__(self) 时的 next() 方法:
显示以下错误
Traceback (most recent call last):
File "class.py", line 20, in <module>
for l in x:
TypeError: instance has no next() method
任何人都可以阐明这种行为。我在 Mark Pilgrim 的 Python 3 书中看到了一个例子,它使用了__next__ 方法。甚至这个例子也没有在我的解释器上运行。
感谢您抽出时间来帮助我!
【问题讨论】:
标签: python-2.6