Location>code7788 >text

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 40 Python's Common Compound Data Types - Enumeration and Use of the enum Module

Popularity:198 ℃/2024-08-28 01:03:18

The Web's Best Object-Oriented Programming Tutorials for Getting Started: 40 Python's Common Compound Data Types - Enumeration and Use of the enum Module

image

Abstracts:

In Python, an enumeration (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 instead of magic numbers or strings in code, and Python provides the enum module to implement enumeration types.

Link to original article:

FreakStudio's Blog

Past Recommendations:

You're learning embedded and you don't know how to be object oriented?

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 00 Introduction to Object-Oriented Design Methods

The network's most suitable for the introduction of object-oriented programming tutorials: 01 Basic Concepts of Object-Oriented Programming

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 02 Python Implementations of Classes and Objects - Creating Classes with Python

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 03 Python Implementations of Classes and Objects - Adding Attributes to Custom Classes

The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 04 Python Implementation of Classes and Objects - Adding Methods to Custom Classes

The Best Object-Oriented Programming Tutorial on the Net for Getting Started: 05 Python Implementation of Classes and Objects - PyCharm Code Tags

The best object-oriented programming tutorials on the net for getting started: 06 Python implementation of classes and objects - data encapsulation of custom classes

The best object-oriented programming tutorial on the net for getting started: 07 Python implementation of classes and objects - type annotations

The best object-oriented programming tutorials on the net for getting started: 08 Python implementations of classes and objects - @property decorator

The best object-oriented programming tutorials on the net for getting started: 09 Python implementation of classes and objects - the relationship between classes

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 10 Python Implementations of Classes and Objects - Class Inheritance and Richter's Replacement Principle

The best object-oriented programming tutorials on the net for getting started: 11 Python implementation of classes and objects - subclasses call parent class methods

The network's most suitable for the introduction of object-oriented programming tutorials: 12 classes and objects of the Python implementation - Python using the logging module to output the program running logs

The network's most suitable for the introduction of object-oriented programming tutorials: 13 classes and objects of the Python implementation - visual reading code artifacts Sourcetrail's installation use

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 14 Python Implementations of Classes and Objects - Static Methods and Class Methods for Classes

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 15 Python Implementations of Classes and Objects - __slots__ Magic Methods

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 16 Python Implementations of Classes and Objects - Polymorphism, Method Overriding, and the Principle of Open-Close

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 17 Python Implementations of Classes and Objects - Duck Types and "file-like objects"

The network's most suitable for the introduction of object-oriented programming tutorials: 18 classes and objects Python implementation - multiple inheritance and PyQtGraph serial data plotting graphs

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 19 Python Implementations of Classes and Objects - Using PyCharm to Automatically Generate File Annotations and Function Annotations

The best object-oriented programming tutorials on the web for getting started: 20 Python implementation of classes and objects - Combinatorial relationship implementation and CSV file saving

The best introductory object-oriented programming tutorials on the net: 21 Python implementation of classes and objects - Organization of multiple files: modulemodule and packagepackage

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 22 Python Implementations of Classes and Objects - Exceptions and Syntax Errors

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 24 Python Implementations of Classes and Objects - Exception Catching and Handling

The best object-oriented programming tutorials on the web for getting started: 25 Python implementation of classes and objects - Python to determine the type of input data

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 26 Python Implementations of Classes and Objects - Context Managers and with Statements

The best introductory object-oriented programming tutorials on the web: 27 Python implementation of classes and objects - Exception hierarchy and custom exception class implementation in Python

The best object-oriented programming tutorials on the net for getting started: 28 Python implementations of classes and objects - Python programming principles, philosophies and norms in a big summary

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 29 Python Implementations of Classes and Objects - Assertions and Defensive Programming and Use of the help Function

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 30 Python's Built-In Data Types - the root class of object

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 31 Python's Built-In Data Types - Object Object and Type Type

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 32 Python's Built-in Data Types - Class Class and Instance Instance

The Best Object-Oriented Programming Tutorials for Getting Started on the Web: 33 Python's Built-In Data Types - The Relationship Between the Object Object and the Type Type

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 34 Python's Built-In Data Types - Python's Common Compound Data Types: Tuples and Named Tuples

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 35 Python's Built-In Data Types - Document Strings and the __doc__ Attribute

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 36 Python's Built-In Data Types - Dictionaries

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 37 Python's Common Composite Data Types - Lists and List Derivatives

The Best Object-Oriented Programming Tutorials on the Net for Getting Started: 38 Python's Common Composite Data Types - Using Lists to Implement Stacks, Queues, and Double-Ended Queues

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 39 Python Common Composite Data Types - Collections

More highlights to watch:

Accelerating Your Python: A Quick Guide to Python Parallel Computing

Understanding CM3 MCU Debugging Principles in One Article

Liver half a month, embedded technology stack summary out of the big

The "Secrets of the Martial Arts" of the Computer Competition

A MicroPython open source project collection: awesome-micropython, including all aspects of Micropython tool library

Avnet ZUBoard 1CG Development Board - A New Choice for Deep Learning

SenseCraft Deploys Models to Grove Vision AI V2 Image Processing Module

Documentation and code acquisition:

The following link can be accessed to download the document:

/leezisheng/Doc

image

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, with common serial port data sending and receiving, data processing, and dynamic graph drawing as application examples for the host computer and the lower computer, and using Sourcetrail code software to visualize and read the code for readers' easy understanding.

The link to get the relevant sample code is below:/leezisheng/Python-OOP-Demo

main body (of a book)

Enumerations are used to define a set of constants with discrete values (i.e., to name a set of related constants in a program)It can make the data more concise and readable. Enumerations are very common in daily life, for example, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, which represent days of the week, are an enumeration.

When using enumeration, you need to pay attention to the following points: first, the enumeration value is a constant, not a variable. It cannot be re-assigned with an assignment statement in the program; second, the enumeration element itself is defined by the system as a numerical value that represents a serial number, defined as 0, 1, 2... in order from 0; finally, it is important to note that only the enumeration value can be assigned to the enumeration variable, and that the element's numerical value cannot be assigned directly to the enumeration variable.

Python's basic datatypes do not support enumerations, but it does provide the enum module. It implements the Enum class, which is used to define unique read-only sets of sequences. enum() returns an enumeration class, with the first argument being the name of the enumeration class, and the second, separated by spaces, being the attributes of the enumeration class, which are auto-numbered starting at one. Note that these properties are read-only.

In the MasterClass host class, we define the following commands:

_# Class variables: _
    _# START_CMD - start command -0_
    _# STOP_CMD - Shutdown command -1_
    _# SENDID_CMD - SEND ID command -2_
    _# SENDVALUE_CMD - Send data command -3_
    START_CMD, STOP_CMD, SENDID_CMD, SENDVALUE_CMD = (0, 1, 2, 3)

Next, we implement it using enum classes, first define a CMD class, which inherits from the Enum class, and in the CMD class, we define the enum members and assign a name and an associated value to each member. The code is as follows:

_# Enum class defining commands_.
class CMD(Enum).
    START_CMD = 0
    STOP_CMD = 1
    SENDID_CMD = 2
    SENDVALUE_CMD = 3
_# Access to enumeration members _
print(CMD.START_CMD)
_# Access the value of an enumeration member _
print(CMD.START_CMD.value)

The results of the run are as follows:

image

We can also define enumerated classes for CMDs using the following statement:

CMD = Enum('CMD', 'START_CMD STOP_CMD SENDID_CMD SENDVALUE_CMD')

When working with enumerated classes, we need to be aware of theTry to avoid hard-coding the values of enumeration members in your code.If you need to use the value of an enumeration member, it is better to use the enumeration member itself rather than its value. This improves the readability of the code and makes it easier to maintain. Also.The names of enumeration members should normally be in uppercase letters, in order to distinguish them from regular variable and function names. This is a convention that helps improve the readability of the code. And.The value of a member of an enumerated class does not have to be an integer, but can also be of another value type, such as a string.

The enum module also includes four classes, IntEnum, Flag, IntFlag, auto, etc. The specific roles are as follows:

resemble corresponds English -ity, -ism, -ization
IntEnum IntEnum inherits from int and Enum, it only allows the value of the enumeration to be an integer.
Flag The value of a flag can only be an integer and supports bitwise operators.
IntFlag IntFlag inherits from int and Flag, so it has the characteristics of both int and Flag.
auto If the value of the enumeration is not important, you can use the instantiation of the auto class, which is automatically given an integer value.

The unique decorator can automatically check for duplicate values in a definition.

@unique
class CMD(Enum):
    START_CMD       =   0
    STOP_CMD        =   1
    SENDID_CMD      =   3
    SENDVALUE_CMD   =   3

The results of the run are as follows:

image

image