Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `object` an instance of `type` and `type` an instance of `object`?

Tags:

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 
like image 930
felixinho Avatar asked Apr 20 '19 15:04

felixinho


People also ask

Is object an instance of type?

"The classes object and type have a unique relationship: object is an instance of type, and type is a subclass of object.

What is the difference between an object and an instance of an object?

The Object is an actual thing that is built based on the 'blue print' (like the house). An instance is a virtual copy (but not a real copy) of the object.

Why object is an instance of a class?

Whenever you run a program, it's an instance of that program. In languages that create objects from classes, an object is an instantiation of a class. That is, an object is a member of a given class with specified values rather than variables.

How an object is identified as an instance of another object Why?

An instance is a specific representation of an object. An object is a generic thing while an instance is a single object that has been created in memory. Usually an instance will have values assigned to it's properties that differentiates it from other instances of the type of object.


1 Answers

Answers to all your questions can be found in this book: Python Types and Objects

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

like image 128
sanyassh Avatar answered Sep 29 '22 05:09

sanyassh