简体   繁体   中英

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?

在此处输入图片说明

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'>

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM