The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 33 Python's Built-in Data Types – The Relationship Between Object and Type
An object is an instance of a certain type, an entity in memory that has a type and attributes; in Python, types themselves are also objects, and all types (classes) are instances of the type type.
This document mainly introduces how to conduct Object Oriented programming with Python, and it requires readers to have a basic understanding of Python syntax and single-chip microcomputer development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications. It takes the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples, and uses the Sourcetrail code software to visually read the code to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Body Text
object and type are built-in objects within the CPython interpreter, and they hold a very special status as the top-level elements of the Python language:
object is the base class of all other objects, and object itself has no base class; its data type is defined as type.
type inherits from object, and all type objects are its instances, including itself. To determine whether an object is a type object, just check if it is an instance of type.
Now going back to the question at the beginning, the built-in method isinstance () essentially determines the data type of an object, and it will trace back to the base class until it reaches object; in CPython, it ultimately calls the following function:
static int
type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b)
{
do {
if (a == b)
return 1;
a = a->tp_base;
} while (a != NULL);
return (b == &PyBaseObject_Type);
}
print(isinstance(object, type)) # 1
print(isinstance(type, object)) # 2
print(isinstance(type, type)) # 3
print(isinstance(object, object))# 4
print(object.__class__) # <class 'type'>
print(type.__class__) # <class 'type'>
We can take a look at what the type class and the object class are respectively in the source code:
It represents the object type of this class structure. In CPython, `object` and `type` correspond to the `PyBaseObject_Type` and `PyType_Type` variables of the `PyTypeObject` type (a wrapper for `PyObject`) respectively. The member `ob_type` used to represent the type is a pointer, both pointing to `PyType_Type`. Therefore, the object types of both `object` and `type` are `type`.(The object class sets its type (i. e., the entity that instantiates this class) to the type class, and the type class sets its own type to itself.)
Then during the initialization process of the type class, the following code is executed:
type->tp_base = &PyBaseObject_Type;
Convert to Python:
type.__base__ = (object,)
indicates that the object class is designated as the parent class of the type class.