Labs / Python OOP

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 08 Python Implementation of Classes and Objects - @property Decorator: Wrapping Methods into Attributes

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

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.

FreakStudio's Blog

Previous Recommendations:

You who are learning embedded systems still don't know Object Oriented? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 00 Introduction to Object Oriented Design Methodology

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 01 Basic Concepts of Object Oriented Programming

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 03 Python Implementation of Classes and Objects – Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 04 Python Implementation of Classes and Objects – Adding Methods to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 05 Python Implementation of Classes and Objects - PyCharm Code Tags

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 06 Python Implementation of Classes and Objects - Data Encapsulation of Custom Classes

The most beginner-friendly Object Oriented programming tutorial on the entire network: 07 Python implementation of classes and objects - type annotations

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

After half a month of dedicated grinding, the ultimate summary of embedded technology stack is now released

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

1 0 19 4

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:

The Most Beginner-Friendly Object Oriented Programming Course on the Whole Network: 06 Python Implementation of Classes and Objects - Data Encapsulation of Custom Classes

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 Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 08 Python Implementation of Classes and Objects - @property Decorator: Wrapping Methods into Attributes

The property decorator is mainly applied in the following scenarios:

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

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 08 Python Implementation of Classes and Objects - @property Decorator: Wrapping Methods into Attributes