How do I subclass collections.Iterator?
-
02-10-2019 - |
Question
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.
Solution
Looks like you are using Python 3.x. Your code works fine on Python 2.x.
>>> import collections
>>> class DummyClass(collections.Iterator):
... def next(self):
... return 1
...
>>> x = DummyClass()
>>> zip(x, [1,2,3,4])
[(1, 1), (1, 2), (1, 3), (1, 4)]
But on Python 3.x, you should implement __next__
instead of next
, as shown in the table of the py3k doc. (Remember to read the correct version!)
>>> import collections
>>> class DummyClass(collections.Iterator):
... def next(self):
... return 1
...
>>> x = DummyClass()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can’t instantiate abstract class DummyClass with abstract methods __next__
>>> class DummyClass3k(collections.Iterator):
... def __next__(self):
... return 2
...
>>> y = DummyClass3k()
>>> list(zip(y, [1,2,3,4]))
[(2, 1), (2, 2), (2, 3), (2, 4)]
This change is introduced by PEP-3114 — Renaming iterator.next()
to iterator.__next__()
.