Why is `object` an instance of `type` and `type` an instance of `object`?
Asked Answered
M

4

33

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
Mirabel answered 20/4, 2019 at 15:57 Comment(0)
A
25

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

Aground answered 20/4, 2019 at 17:46 Comment(7)
Sadly, that image is missing an arrow, to reflect the fact that issubclass(object, object) is true.Sawyer
@Sawyer didn't you forget that every class is subclass of itself and it is not special for object class?Aground
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.)Sawyer
The link to the book by Cannata seems to be dead.Aliquot
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.Code
Thank you so much - very nice information.Nunciature
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)Tamaru
T
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.

Threaten answered 11/10, 2019 at 3:28 Comment(0)
D
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/

Dropkick answered 20/4, 2019 at 16:16 Comment(1)
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.Mirabel
W
-1

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

Waistband answered 14/4, 2022 at 4:26 Comment(1)
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.Yingyingkow

© 2022 - 2024 — McMap. All rights reserved.