Location>code7788 >text

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

Popularity:437 ℃/2024-08-11 00:30:37

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

image

Abstracts:

In Python, a docstring is a string used to document a module, class, method, or function, usually placed at the beginning of a definition, immediately after a declaration. A docstring is surrounded by triple quotes (""" or ''') and can span multiple lines.

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

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

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)

In Python, we can add documentation strings to methods, classes, and modules, which can be accessed through a special attribute ___.doc__Get. A documentation string (docstring) is a string used to describe a class, function, module, or method, usually at the top of a definition, and is used to provide information about its function and usage. A docstring is usually enclosed in triple quotes (''' or """) and is located at the top of a class, function, module, or method definition.

For example, in the following example, we add the necessary comments to the MasterClass host class and its StartMaster open host method, accessed by the following statement:

_# Access the __doc__ attribute of the MasterClass class.
    print(MasterClass.__doc__)
    _# Access the __doc__ property of the StartMaster method of the MasterClass class _# Access the __doc__ property of the MasterClass class
    print(. __doc__)

The sample code is as follows:

class MasterClass(SerialClass,PlotClass).
    '''
    MasterClass: this class represents the host class, mainly used to receive sensor data, send and receive commands, etc.

    It has the following attributes:
        state -- indicates the working state of the host
        port -- indicates the port number of the host.
        wintitle -- the window title
        ... ...

    Has the following methods:
        StartMaster -- starts the host
        StopMaster -- stops the host
        RecvSensorValue -- receive sensor data value
        ... ...
    '''
    ...
    _# Initialization of class _
    def __init__(self,state:int = IDLE_STATE,port:str = "COM17",wintitle:str="Basic plotting examples",plottitle:str="Updating plot",width:int = 1000,height:int=600).
        pass
    _# StartMaster
    def StartMaster(self):
        '''
            StartMaster method - turning on the host
            Calling the () method
        :return: no return value
        '''
        pass

The following is the result of the run:

image

At the same time, we can also add a document string to the named tuple. Use a class to inherit the corresponding named tuple, and add a document comment in the class, the sample code is as follows:

from collections import namedtuple

_Sensor = namedtuple("Sensor", "ID CURRENTVALUE MAXVALUE MINVALUE")

class Sensor(_Sensor).
    '''
        This is a class that inherits from the named tuple representing the sensor data
        It has four attributes:
            (1) ID : Indicates the ID number of the sensor.
            (2) CURRENTVALUE : the current value of the data collected by the sensor
            (3) MAXVALUE: The maximum data value collected by the sensor.
            (4) MINVALUE: The minimum data value collected by the sensor.
    '''
sensor_tuple=Sensor(ID="16",CURRENTVALUE=32,MAXVALUE=62,MINVALUE=2)
print(sensor_tuple.__doc__)

The results of the run are as follows:

image

image