[Python-ideas] PEP draft: Unifying function/method classes

Nick Coghlan ncoghlan at gmail.com
Sun Apr 1 05:07:50 EDT 2018


On 1 April 2018 at 02:58, Jeroen Demeyer <J.Demeyer at ugent.be> wrote:
> On 2018-03-31 18:09, Steven D'Aprano wrote:
>> Seems to me that if you want a fast, exact (no subclasses) check, you
>> should use "type(obj) is Class" rather than isinstance. If the *only*
>> reason to prohibit subclassing is to make isinstance a bit faster,
>> I don't think that's a good enough reason.
>
> I didn't really mean "isinstance" literally, I was mostly thinking of the C
> API. I agree that it's not clear.
>
> Do you happen to know why the existing function classes in Python disallow
> subclassing? I assumed that it was for exactly this reason.

Disallowing subclasses is a simplifying assumption for builtin types,
since it means they don't need to account for subclasses potentially
failing to enforce class invariants.

That said, allowing Cython/CFFI/etc to use the existing fast paths for
native Python functions and/or builtin C functions is a reasonable
justification for flipping that switch and doing the extra work needed
to make it robust - it's just likely to involve adding a number of
`*_CheckExact()` calls in various places.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list