[Python-Dev] semantics of subclassing things from itertools

Maciej Fijalkowski fijall at gmail.com
Sun Sep 13 21:09:56 CEST 2015


On Sun, Sep 13, 2015 at 5:46 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
>
>> 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 Py_TPFLAGS_BASETYPE 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

Well, fair enough, but the semantics of "whatever happens to happen
because we decided subclassing is a cool idea" is possibly the worst
answer to those questions. Ideally, make it non-subclassable. If you
want to have it subclassable, then please have defined semantics as
opposed to undefined.


More information about the Python-Dev mailing list