According to the documentation on ABCs, I should just have to add a next method to be able to subclass collections.Iterator. So, I’m using the following class:
class DummyClass(collections.Iterator):
def next(self):
return 1
However, I get an error when I try to instantiate it:
>>> x = DummyClass()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class DummyClass with abstract methods __next__
I’m guessing that I’m doing something stupid, but I can’t figure out what it is. Can anyone shed some light on this? I could add a __next__ method, but I was under the impression that was only for C classes.
Looks like you are using Python 3.x. Your code works fine on Python 2.x.
But on Python 3.x, you should implement
__next__instead ofnext, as shown in the table of the py3k doc. (Remember to read the correct version!)This change is introduced by PEP-3114 — Renaming
iterator.next()toiterator.__next__().