0

As I know, subclass should be subclass of object, but why it also is an instance of object?

hmank ~ » python
Python 3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...   pass
... 
>>> isinstance(A, object)
True
>>> issubclass(A, object)
True
>>> 

What's more, I quote from @prosti answer that isinstance

Returns a Boolean stating whether the object is an instance or subclass of another object.

But the examples on the same link shows

>>> class Foo: pass
...
>>> class Bar(Foo): pass
...
>>> isinstance(Bar, Foo)
False
>

Seems it means a class1 is an instance of class2, if and only if class2 is object, right?

roachsinai
  • 527
  • 5
  • 14

3 Answers3

1

In python3 all classes derive from object thus

class A(object):
    pass

and

class A:
    pass

are identical.
Regarding why isinstance(A, object) returns True
see the following code

class A: 
    @classmethod 
    def do(cls): 
        print(f"hello from {cls.__name__}") 

A.do()                                                                  

# hello from A
Zubda
  • 943
  • 1
  • 6
  • 16
  • It's not asking why `A` is derived from object. It's asking why the class itself is an instance of object. – khelwood May 05 '19 at 17:09
  • a class definition is an object as it can have class methods and properties disregarding of whether it has been instantiated. – Zubda May 05 '19 at 17:15
  • That would have been more like an answer to the OP's question. – khelwood May 05 '19 at 19:49
1

Because everything in python is treated as an object, which is a nicer way to say, every object is an instance of object in python!

If you think about it, it makes total sence,since python is an object oriented language, and because of that, it is be normal and expected that every value is an object


In [9]: class A: 
   ...:     pass 
   ...:                                                                                                                                               

In [10]: isinstance(A, object)                                                                                                                        
Out[10]: True

In [11]: isinstance(A(), object)                                                                                                                      
Out[11]: True

In [12]: isinstance(1, object)                                                                                                                        
Out[12]: True

In [13]: isinstance([2,3], object)                                                                                                                    
Out[13]: True

In [14]: isinstance('hello', object)                                                                                                                  
Out[14]: True
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • I think it would be more properly stated as: every object is an instance of a **class**. – martineau May 05 '19 at 18:35
  • @martineau but for my question is why a **class** is an instance of **object**. – roachsinai May 06 '19 at 00:45
  • @roachsinai: Because in Python, classes are objects, too. They're instances of [Metaclasses](https://docs.python.org/3/reference/datamodel.html#metaclasses). – martineau May 06 '19 at 02:21
  • @martineau Yeah, classes are instances of Metaclass. But **object** is also an instance of Metaclass. The Metaclass means **type**. So why classes is also an instance of **object**? – roachsinai May 06 '19 at 02:32
  • @roachsinai: Because, almost mystically, here's some circular logic in the type system. – martineau May 06 '19 at 14:07
1

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.

prosti
  • 42,291
  • 14
  • 186
  • 151
  • So, why `isinstance` support to check whether the object is subclass of another object? I mean there is some overlap between `issubclass`. – roachsinai May 06 '19 at 00:57
  • And the example at the link you post seems support that `isinstance(A, object)` is `True` only when the class parameter is `object`. https://python-reference.readthedocs.io/en/latest/docs/functions/isinstance.html#example-2 – roachsinai May 06 '19 at 01:03