[Python-Dev] semantics of subclassing things from itertools (original) (raw)

Raymond Hettinger raymond.hettinger at gmail.com
Sun Sep 13 17:46:31 CEST 2015


On Sep 13, 2015, at 3:49 AM, Maciej Fijalkowski <fijall at gmail.com> wrote:

The intended semantics are that the itertools are classes (not functions and not generators). They are intended to be sub-classable (that is why they have PyTPFLAGSBASETYPE defined). Ok, so what's completely missing from the documentation is what are the semantics of subclasses of those classes? Can you override any magic methods? Can you override next (which is or isn't a magic method depending how you look)? Etc. The documentation on this is completely missing and it's left guessing with "whatever cpython happens to be doing".

The reason it is underspecified is that this avenue of development was never explored (not thought about, planned, used, tested, or documented). IIRC, the entire decision process for having Py_TPFLAGS_BASETYPE boiled down to a single question: Was there any reason to close this door and make the itertools not subclassable?

For something like NoneType, there was a reason to be unsubclassable; otherwise, the default choice was to give users maximum flexibility (the itertools were intended to be a generic set of building blocks, forming what Guido termed an "iterator algebra").

As an implementor of another version of Python, you are reasonably asking the question, what is the specification for subclassing semantics? The answer is somewhat unsatisfying -- I don't know because I've never thought about it. As far as I can tell, this question has never come up in the 13 years of itertools existence and you may be the first person to have ever cared about this.

Raymond



More information about the Python-Dev mailing list