The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 40 Common Composite Data Types in Python - Enumerations and Usage of the enum Module
In Python, an enumeration (often abbreviated as Enum) is a composite data type used to represent a set of related but distinct constant values. Enumeration types allow us to use human-readable names in place of magic numbers or strings in code, and Python provides the `enum` module to implement enumeration types.
This document mainly introduces how to use Python for Object Oriented programming, which requires readers to have a basic understanding of Python syntax and microcontroller 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, taking 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. Meanwhile, it uses the Sourcetrail code software to visualize the code for reading to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Body Text
An enumeration is used to define a set of constants with discrete values (i. e., to assign names to a set of related constants in a program), which makes data more concise and readable. Enumerations are very common in daily life; for example, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY, which represent the days of the week, form an enumeration.
When using enumerations, the following points should be noted: First, enumeration values are constants, not variables, and you cannot reassign them using assignment statements in a program; second, each enumeration element is assigned a sequence number by the system by default, starting from 0 and incrementing sequentially as 0,1, 2,. ..; finally, it should be noted that you can only assign enumeration values to enumeration variables, and cannot directly assign the numeric value of an element to an enumeration variable.
Python's basic data types do not natively support enumerations, but it provides the `enum` module, which implements the `Enum` class for defining unique, read-only sets of sequences. `Enum ()` returns an enumeration class: the first parameter specifies the name of the enumeration class, while the second parameter, which is space-separated, lists the attributes of the enumeration class, and these attributes are automatically numbered starting from 1. Note that all these attributes are read-only.
In the MasterClass host class, we have defined the following commands:
Next, we use an enumeration class to implement this. First, define a CMD class that inherits from the Enum class. Inside the CMD class, we define enumeration members and assign a name and an associated value to each member. The code is as follows:
# Defines the enumeration class of commands
class CMD(Enum):
START_CMD = 0
STOP_CMD = 1
SENDID_CMD = 2
SENDVALUE_CMD = 3
# Access an enumeration member
print(CMD.START_CMD)
# Access the value of an enumeration member
print(CMD.START_CMD.value)
The running results are as follows:
We can also use the following statement to define the enumeration class of CMD:
When using enum classes, we should note that we should try to avoid hardcoding the values of enum members in the code. If we need to use the values of enum members, it is better to use the enum members themselves rather than their values. This can improve the readability of the code and make it easier to maintain. Meanwhile, the names of enum members should usually be in uppercase letters so as to be distinguished from regular variable and function names. This is a convention that helps improve code readability. Furthermore, the values of enum class members do not necessarily have to be integers, and can also be of other value types such as strings.
The enum module also includes four classes: IntEnum, Flag, IntFlag, and auto, with their specific functions listed below:
Class
Function
IntEnum
IntEnum inherits from both int and Enum, and it only allows the values of its enumeration members to be of integer type.
Flag
The value of Flag can only be an integer and supports bitwise operators at the same time.
IntFlag
IntFlag inherits from int and Flag, so it has all the features of both int and Flag.
auto
If the values of the enumeration are not important, you can use the instantiation of the `auto` class, which will automatically assign an integer value to each enumerator.
The unique decorator can automatically check for duplicate values in the definition.