Objects and types in Python are a very important concept. In Python, everything is an object, including numbers, strings, lists, etc. , and each object has its own 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
In the process of learning Python, we will definitely come across a saying: "Everything in Python is an object". All basic operable units in Python, including data types, values, variables, functions, classes, instances and so on, are represented as Objects. Each object has three basic attributes: ID, type and value. That is to say, when an object is stored in a block of memory, this block of memory will definitely contain these three attributes.
a = 1
print(id(a), type(a), a)
print(id(int), type(int), int)
print(id(type), type(type), type)
Here, when we use the built-in id () method, it will retrieve the unique identifier of the object for us, which is an integer that usually corresponds to the memory address of the object. The built-in type () method, on the other hand, is used to obtain the type of the object; although the description here starts with "class", it actually refers to the data type of the object. The above running results are shown below:
Here, a is an object of data type int with a value of 1. Both int and type are objects, and their data type is type. An object may also have one or more base/parent classes; when an object represents a data type, such as the int object, it will have the __bases__ attribute.
Python divides objects into two categories: type objects (Type) and non-type objects (Non-type):
int, type, list, etc. are all type objects that can be inherited and instantiated;
1, [1] and others are all non-type objects; they cannot be further inherited or instantiated, and various operations such as arithmetic operations can be performed between objects according to their respective types.
Type
An object necessarily has a Type attribute, and likewise, Type cannot exist independently of an object. The type of an object defines the behaviors supported by that object as well as the types of values it holds — for example, retrieving a name, performing arithmetic operations, obtaining the length, and so forth. An object of type int only accepts integer values.
The built-in method type () is used to obtain the type of an object. We can also use the class name followed by ". __class__" to get the type of an object, and the two approaches are equivalent.