__subclasscheck__
is the method to override built-in issubclass
.
The call issubclass(X,Y)
first checks whether Y.__subclasscheck__
exists, and if so, calls Y.__subclasscheck__(X)
instead of its normal implementation.
similarly;__instancecheck__
is the method to override built-in isinstance
. The call isinstance(X, Y)
first checks whether
Y.__instancecheck__
exists, and if so, calls Y.__instancecheck__(X)
instead of its normal implementation.
In [121]: class FooProxy3(object):
...: pass
In [122]: issubclass(FooProxy3,IFoo)
Out[122]: False
In [123]: for c in IFoo.implementors:
...: print c
...:
<class '__main__.Foo'>
<class '__main__.FooProxy'>
In [124]: for c in FooProxy3.mro():
...: print c
...:
<class '__main__.FooProxy3'>
<type 'object'>
In [125]: IFoo.register(FooProxy3)
In [126]: for c in IFoo.implementors:
...: print c
...:
<class '__main__.Foo'>
<class '__main__.FooProxy'>
<class '__main__.FooProxy3'>
In [127]: issubclass(FooProxy3,IFoo)
Out[127]: True
lets consider above example;create new class FooProxy3
; and check issubclass
for that; and it will return False
(as per Out[122]
) because we are looking for subclass check in IFoo.implementors
; and there it is not present (as per In[123]
)
but when we have register
FooProxy
(here it means; now we can see it IFoo.implementors
); we can see it Foxproxy3
in IFoo.implementors
(as per In[126]
)
so when we check issubclass
; it wil return True
.
for more info;
Customizing instance and subclass checks
Overloading isinstance() and issubclass()