Not a bad question.
class A:
pass
a = A()
print(isinstance(a, A)) #True
print(isinstance(object, A)) #False
print(isinstance(A, object)) #True
print(issubclass(A, A)) #True
print(issubclass(A, object)) #True
By definition, isinstance
:
Returns a Boolean stating whether the object is an instance or subclass of another object.
On the other hand issubclass
:
Returns a Bool type indicating whether an object is a subclass of a class.
With additional remark that a class is considered a subclass of itself.
Update:
Seems it means a class1 is an instance of class2, if and only if class2 is object, right?
You get answers by testing, and logic is super simple. A class is a class and object is an instance of a class.
You can check the code in case you really need to understand the implementation.
Also you may find the test cases if you are a geek.
The object must be instantiated in order to classify for True
in the following examples:
class Foo: pass
class Bar(Foo): pass
print(isinstance(Bar(), Foo)) #True
print(isinstance(Bar(), Bar)) #True
print(Bar) #<class '__main__.Bar'>
print(Bar()) #<__main__.Bar object at 0x7f9fc8f32828>
Also, some examples in here are Python3 specific, if you are Python2 guy, you must know that you should be more explicit and write:
class Bar(object): pass
The (object)
part is a must if you write Python agnostic code.
Lastly check Standard on Overloading isinstance() and issubclass() but have in mind standards are "live" and may update in the future.
Lastly you may check this on classes objects relation.