You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Removing this will likely harm performance, and I'm sure people are relying on this in some way already.
I think it would be better to update the documentation. Do you have a concrete example of where it would be useful for a type to declare an instance of itself as not an instance, other than party tricks?
Reacted by Brian Schubert, Adam Turner and Serhiy Storchaka
Personally, I deem it to be a feature that makes it easier to reason about programs. As suggested in the other issue, the docs could say, "Note that any object x is always considered to be an instance of type(x), and this cannot be overridden."
This makes isinstance reliable. It wouldn't be fun to write type(x) is y or isinstance(x, y) to achieve the same effect.
If Guido doesn't respond, this is my effort to channel him:
The __instancecheck__ hook was added for a specific reason, to support virtual subclasses and abstract base classes. This was an extension of isinstance, not a replacement. There was no intention to change core functionality of isinstance(obj, cls) returning True when obj is an actual instance of cls. The new feature that was offered was the ability to return True for duck typed objects. In short, the code referenced above is not an optimization (and certainly not "overzealous"); instead, it reflectes the intended semantics for the target use cases.
I'm with Raymond. Changing this would break any code that uses __instancecheck__ as it has worked since its introduction. (Note that this is not unlike how __getattr__ isn't called if the attribute can be found in the instance dict -- it's an old pattern in Python.) In any case, at this point it's not an optimization, it's got well-understood semantics.
However, the docs are indeed ambiguous and should clarify that if type(x) is C, isinstance(x, C) is always True and doesn't call __instancecheck__ at all.
Activity
ZeroIntensity commentedon Feb 16, 2026
Removing this will likely harm performance, and I'm sure people are relying on this in some way already.
I think it would be better to update the documentation. Do you have a concrete example of where it would be useful for a type to declare an instance of itself as not an instance, other than party tricks?
George-Ogden commentedon Feb 17, 2026
I agree - I don't see any specific use case.
George-Ogden commentedon Feb 17, 2026
I also ran
pyperformanceto see if this optimisation actually helped. In conclusion, removing it (updated) increases performance:rhettinger commentedon Feb 18, 2026
@gvanrossum Was this a bug or feature?
Personally, I deem it to be a feature that makes it easier to reason about programs. As suggested in the other issue, the docs could say, "Note that any object x is always considered to be an instance of type(x), and this cannot be overridden."
This makes
isinstancereliable. It wouldn't be fun to writetype(x) is y or isinstance(x, y)to achieve the same effect.rhettinger commentedon Feb 18, 2026
If Guido doesn't respond, this is my effort to channel him:
gvanrossum commentedon Feb 18, 2026
I'm with Raymond. Changing this would break any code that uses
__instancecheck__as it has worked since its introduction. (Note that this is not unlike how__getattr__isn't called if the attribute can be found in the instance dict -- it's an old pattern in Python.) In any case, at this point it's not an optimization, it's got well-understood semantics.However, the docs are indeed ambiguous and should clarify that if
type(x) is C,isinstance(x, C)is alwaysTrueand doesn't call__instancecheck__at all.George-Ogden commentedon Feb 19, 2026
I have closed the original pull request and opened another one with documentation fixes.
serhiy-storchaka commentedon Jun 3, 2026
Isn't this a duplicate of #79264?