It is really hard to use isinstance
and judge whether a particular variable var
is an instance of a subclass or superclass. What you can in fact do is specify a special property __class__
which can tell you about the instance's class.
for example:
class A:
@property
def __class__(self):
return '__main__.A'
class B(A):
@property
def __class__(self):
return '__main__.B'
b = B()
a = A()
print b.__class__
> '__main__.B'
print a.__class__
> '__main__.A'
If you however inherit class A
with object
you can check the behaviour without specifying these properties.
class A(object):
pass
class B(A):
pass
b = B()
a = A()
print b.__class__
> '__main__.B'
print a.__class__
> '__main__.A'
PS: I referred this SO question although it is in Java