TypeError: iter() returned non-iterator of type 'xx' 從Python3開始,要用__next__
從Python3開始,要用__next__python
http://www.python.org/dev/peps/pep-3114/url
代碼:
class MyIterator(object):
def __init__(self,step):
self.step=step
def next(self):
if self.step==0:
raise StopIteration
self.step-=1
return self.step
def __iter__(self):
return self
for el in MyIterator(5):
print(el)
運行結果:
Traceback (most recent call last):
File "..\pythonadvancedprogramming\advancedThree.py", line 19, in <module>
for el in MyIterator(5):
TypeError: iter() returned non-iterator of type 'MyIterator'
可是把 def next(self): 改成def __next__(self): 就沒有問題了spa