Playing around with isinstance
, I would like to know how it works for a subclass check.
class A:
pass
class B(A):
pass
class C(B):
pass
print(isinstance(C(), C)) # True, I understand this fine
print(isinstance(C(), B)) # True, Ok, but how is this `True`?
print(isinstance(C(), A)) # True, this too?
What are the differences between type() and isinstance()? explains that isinstance
does work for a subclass as seen in my second and third print
statement. My question is not that, but, how does it work? When class A
was defined it had no information but about the existence of class B
or class C
, likewise B
had no information about C
.
According to How does isinstance work for List? __instancecheck__
is called for isinstance
, so if my class A
was created before B
and C
how does this Python know that C()
is indeed an instance of one of the subclass (B
, subclass of A
) or subclass of subclass of A
(C
, which is a subclass of B
which itself is a subclass of A
)?
Does something happen when I inherit a class in Python such that the base class knows that a child class exists? If that is the case can anyone let me know how this works?
PS : Do let me know if any clarification is needed. Many Thanks.