I am trying to learn how iterators, generators and decorators works in python based on the tutorials in this website.
In the first example, he/she demonstrate a simple example as below:
class Count:
def __init__(self, low, high):
self.low = low
self.high = high
def __iter__(self):
return self
def __next__(self):
if self.current > self.high:
raise StopIteration
else:
self.current +=1
return self.current -1
The problem is, I can't iterate over objects of this class:
>>> ================================ RESTART ================================
>>>
>>> c = Count(1, 10)
>>> for i in c:
print i
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
for i in c:
TypeError: instance has no next() method
>>>
What's wrong?