31

I am a little bit confused about the object and type classes in Python 3. Maybe someone can clear up my confusion or provide some additional information.

My current understanding is that every class (except object) inherits from a base class called object. But every class (including object) is also an instance of the class type, which is an instance of itself and object and also inherits from object.

My questions are:

  • Is there a reason/design decision why object is an instance of type and type inherits from object? Has the type/class of an object also to be an object itself?

  • How can a class (type) be an instance of itself?

  • Which one is the real base class object or type?
    I always thought object would be the most "fundamental" class, but it seems to be an instance of type, which is an instance of object, which is an instance of type, ... Where does this recursion end?

  • Is there a possibility to illustrate the relation between the object and the type class?

I tried looking up the entries of object and type in the Documentation of the Python Standard Library.

Every class (except object) inherits from object.

>>> for x in object, int, float, str, list, dict:
...     print(f'{x.__name__:6}: {x.__bases__}')
... 
object: ()
int   : (<class 'object'>,)
float : (<class 'object'>,)
str   : (<class 'object'>,)
list  : (<class 'object'>,)
dict  : (<class 'object'>,)

Every class is an instance of the class type.

>>> for x in object, int, float, str, list, dict:
...     print(f'{x.__name__:6}: {x.__class__}')
... 
object: <class 'type'>
int   : <class 'type'>
float : <class 'type'>
str   : <class 'type'>
list  : <class 'type'>
dict  : <class 'type'>

type is an instance of itself.

>>> type.__class__
<class 'type'>

type also inherits from object.

>>> type.__bases__
(<class 'object'>,)

Also

>>> isinstance(object, type)
True
>>> isinstance(type, object)
True
>>> isinstance(type, type)
True
>>> isinstance(object, object)
True
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
felixinho
  • 625
  • 7
  • 17

4 Answers4

23

Answers to all your questions can be found in this book: Python Types and Objects (could be found also in the: Wayback machine archive)

UPD: another link to the book. Let me know if it dies too.

The most important parts to answer your questions:

  • Has the type/class of an object also to be an object itself?

Yes, according to the Rule 1 from chapter 1:

"Everything is an object... Any classes that we define are objects, and of course, instances of those classes are objects as well."

  • Which one is the real base class object or type?

From chapter 2:

"These two objects are primitive objects in Python. We might as well have introduced them one at a time but that would lead to the chicken and egg problem - which to introduce first? These two objects are interdependent - they cannot stand on their own since they are defined in terms of each other."

Also Luciano Ramalho in his book "Fluent Python" says that this relation can't be expressed in Python (chapter 21):

"The classes object and type have a unique relationship: object is an instance of type, and type is a subclass of object. This relationship is "magic": it cannot be expressed in Python because either class would have to exist before the other could be defined. The fact that type is an instance of itself is also magical."

So, for your question:

  • How can a class (type) be an instance of itself?

Luciano says that it can't be expressed in Python too.

  • Is there a possibility to illustrate the relation between the object and the type class?

Many thanks to the author who made this illustration in сhapter 3:

relation between object, type and other classes

Viktor Ilienko
  • 817
  • 8
  • 15
sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • Sadly, that image is missing an arrow, to reflect the fact that `issubclass(object, object)` is true. – chepner Apr 30 '20 at 19:14
  • 6
    @chepner didn't you forget that every class is subclass of itself and it is not special for `object` class? – sanyassh Apr 30 '20 at 23:10
  • But there is an arrow to emphasize that `type` has type `type`. Just wondering why the same emphasis wasn't made for `object` being a subclass of `object`. (That is, every class has a parent class; but `object` is the unique class which is its own parent.) – chepner May 01 '20 at 00:21
  • The link to the book by Cannata seems to be dead. – Danny Dec 31 '20 at 23:03
  • 2
    `object` is not its own parent in the sense that `type` is its own class: object is the root, and it have no parents. It is a subclass of itself in the same sense that `class C` is a subclass of itself. `class C` bases are `(object,)`, while object bases is empty `()` as seem in a print in the question text. – jsbueno Jan 22 '21 at 18:20
  • Thank you so much - very nice information. – Yibin Lin May 01 '21 at 00:45
  • 1
    Arrow type meaning: Dotted arrows decode "is of type" relationship (e,g. `type(object())` is `object`), solid arrows decode "is subclass of" relationship (`list` is subclass of `object`) – zwithouta Dec 02 '22 at 13:44
2

According to Python Data Model, everything in Python is an object and every object has an identity, a type and a value.

object is the base class for all objects, type is also an object, so it is an instance of object, and same for object itself. Also type is the base class of all object types, so type of object is type, and same for type itself.

This is my understanding, welcome to point out any mistakes.

Chao Chen
  • 71
  • 1
  • 4
1

<class 'type'> is the metaclass of class object, and every class (including type) has inherited directly or indirectly from object.

If you need to find more about metaclasses chack out here https://realpython.com/python-metaclasses/

PMN
  • 493
  • 5
  • 11
  • 2
    Thank you! The article was really helpful. But from what I understand now: The `type` class is not a "normal" metaclass as they "cheated" on the implementation level and as it can't be reproduced in pure Python. – felixinho Apr 20 '19 at 19:22
-1

I realized it was very similar to instanceKlass and _java_mirror in Java

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 08:48