Zero Days
Zero Days

Reputation: 859

What is the relationship between Python 3 built-in types?

In python 3 everything is a object. I drew a diagram about relation of classes. Is this diagram correct?

enter image description here

the hard part is about type and object classes. how are they in relation ? type is a object ? or object is a type ?

>>> x=type
>>> type(x)
<class 'type'>
>>> x=object
>>> type(x)
<class 'type'>

Upvotes: 2

Views: 324

Answers (2)

kolokkol
kolokkol

Reputation: 183

As far as I know, the class relations are kind of like this in Python 3:

  • Every class is a subclass of object
  • Every class is an instance of type

Every class is created by the type class or an other metaclass, which derives from type. Because of this every class is an instance of type (including type!) Every class will return True for isinstance(cls, type).

In Python 3, every class is also a subclass from object. Every class or instance will return True for isinstance(cls_or_instance, object)

A special case is metaclasses. A metaclass derives from type, so every metaclass will return True for issubclass(metaclass, type) and isinstance(metaclass, type)

Upvotes: 3

skyking
skyking

Reputation: 14390

The type object is itself an object. Note though that the inheritance model of python is not the same as in other OO-languges, much of it depends on duck typing rather than inheritance.

Note that type(x) returns the type of the object, that type(object) returns <class 'type'> means nothing more than the type of object (which is the type all objects has) is a type (the type all types are), type itself is a type so the type of it is again type.

Upvotes: 0

Related Questions