Contents
Abstract:
This paper mainly introduces the basic definition, application scenarios and usage of the @property decorator, and also presents the application scenarios and syntax of the setter decorator and deleter decorator.
Source Link:
Previous Recommendations:
You who are learning embedded systems still don't know Object Oriented? ?!
More exciting content can be found here:
Speed Up Your Python: A Quick Guide to Python Parallel Computing
Understand the Debugging Principle of CM3 Microcontroller in One Article
The "Secret Martial Arts Manual" for Computer Science Competitions
Access to Documents and Code
You can access the following link to download the document:
https://github.com/leezisheng/Doc

This document mainly introduces how to use Python for Object Oriented programming, and 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. It takes the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the lower 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
Link to the previous article:
In the above program, the `@property` decorator is applied to the `devstate ()` method, which turns this method into the getter method for the `devstate` property.
Here, use the following statement to directly access the method by its method name:
s = SerialClass()
print(s.devstate)The program runs as follows:

The property decorator is mainly applied in the following scenarios:
- the encapsulation feature of the protected access specifier
- allow developers to manipulate class attributes using the "object. attribute" syntax
The syntax format of @property is as follows:
@property
def 方法名(self)
代码块Similarly, if you want to implement the modification of the value of the devstate attribute, you also need to add a setter method for the attribute, which requires the use of the setter decorator.
Its syntax format is as follows:
@方法名.setter
def 方法名(self, value):
代码块In addition, you can also use the deleter decorator to delete a specified attribute, and its syntax format is as follows:
@方法名.deleter
def 方法名(self):
代码块For a more detailed introduction to decorators, please refer to the following link; we will not elaborate further here:
https://www.runoob.com/w3cnote/python-func-decorators.html
