1

Let me first show how isinstance() works

class superclass:
    def __init__(self, var):
        self.var = var

class subclass(p):
    pass

obj = subclass("pinoy")

This is how isinstance works

>>> isinstance (obj, superclass)
True

Here, obj is mainly a instance of subclass. Since, subclass inherits from superclass,

isinstance(obj, superclass) returns True

Is there any, way that would check if a object mainly belongs to the class specified and return Flase otherwise.

Artaza Sameen
  • 519
  • 1
  • 5
  • 13
  • Does this answer your question? [What are the differences between type() and isinstance()?](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance) – tevemadar Dec 03 '19 at 08:38

1 Answers1

3

You could use type:

class superclass:
    def __init__(self, var):
        self.var = var

class subclass(superclass):
    pass
obj = subclass("pinoy")

print(type(obj))
#<class '__main__.subclass'>

type(obj) == subclass
# True

type(obj) == superclass
# False
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50