Labs / Python OOP

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 29 Python Implementation of Classes and Objects – Assertions, Defensive Programming and the Usage of the help function

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

Contents
The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 29 Python Implementation of Classes and Objects – Assertions, Defensive Programming and the Usage of the help function

Abstract:

In Python, assertion is a commonly used debugging tool that allows programmers to write a statement to check a certain condition. This article mainly introduces the application scenarios and features of assertion as well as the usage of the assert statement, and also presents defensive programming and the help () function.

FreakStudio's Blog

Previous Recommendations:

You who study 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 Online: 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 Course on the Whole Network: 04 Python Implementation of Classes and Objects – Adding Methods to Custom Classes

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

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 Whole Network: 07 Python Implementation of Classes and Objects - Type Annotations

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

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 09 Python Implementation of Classes and Objects - Relationships Between Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 10 Python Implementation of Classes and Objects - Class Inheritance and Liskov Substitution Principle

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 12 Python Implementation of Classes and Objects - Using Python's logging Module to Output Program Runtime Logs

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 13 Python Implementation of Classes and Objects – Installation and Usage of Sourcetrail, a Fantastic Tool for Visual Code Reading

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 14 Python Implementation of Classes and Objects - Static Methods and Class Methods of Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 15 Python Implementation of Classes and Objects - The __slots__ Magic Method

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 16 Python Implementation of Classes and Objects - Polymorphism, Method Overriding and Open Closed Principle

The most beginner-friendly Object Oriented programming tutorial across the network: 17 Python implementation of classes and objects - duck typing and "file-like object"

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 18 Python Implementation of Classes and Objects - Multiple Inheritance and Serial Port Data Curve Plotting with PyQtGraph

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 19 Python Implementation of Classes and Objects – Using PyCharm to Automatically Generate File Comments and function Comments

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 20. Python Implementation of Classes and Objects – Implementation of Composition Relationship and CSV File Saving

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 21 Python Implementation of Classes and Objects - Multi-File Organization: Module and Package

The Best Entry-Level Object Oriented Programming Tutorial on the Whole Network: 22 Python Implementation of Classes and Objects - Exceptions and Syntax Errors

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 23 Python Implementation of Classes and Objects - Throwing Exceptions

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 24 Python Implementation of Classes and Objects - Exception Catching and Handling

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 25 Python Implementation of Classes and Objects - Python Determines Input Data Types

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 26 Python Implementation of Classes and Objects - Context Managers and the with Statement

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 27 Python Implementation of Classes and Objects - Implementation of Exception Hierarchy and Custom Exception Classes in Python

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 28 Python Implementation of Classes and Objects - A Comprehensive Summary of Python Programming Principles, Philosophy and Specifications

For more exciting content, please check out:

Speed Up Your Python: A Quick Guide to Python Parallel Computing

Mastering the Debugging Principles of CM3 Microcontrollers 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

Document and Code Access:

You can download the document via the following link:

https://github.com/leezisheng/Doc

19

This document mainly introduces how to conduct Object Oriented programming with Python, and it requires readers to have a basic understanding of Python syntax and single-chip microcomputer 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 slave 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

assertion (assert)

In Python programming, assert, as a powerful debugging tool, is widely used in the process of code debugging and development. By inserting assert statements in the code, we can conduct strict checks on the correctness of the code. When the condition of an assert evaluates to false, the program will raise an AssertionError exception and stop execution. When the condition of an assert evaluates to true, the program will continue execution as normal, as if the assert statement had never existed. Therefore, the rational use of assert is of great significance for ensuring the quality and stability of code.

assert expression, description

Take the following code as an example: you can see that the result of "1== 2" is False, and an AssertionError is thrown.

1 2

Unlike exceptions, assertions are designed to alert developers that an unrecoverable error has likely occurred in the program. For certain predictable errors, such as a relevant file not being found, users can typically correct the issue on their own or retry the operation. Assertions, however, are not intended for such problems. In a bug-free program, assertion conditions will never be triggered. But if an assertion is violated, the program will crash and report an error, clearly pointing out to the developer exactly which "impossible" situation has arisen. This makes it much easier to track down and fix bugs in the program.In Python, the assert statement is an auxiliary function for debugging, rather than a mechanism for handling runtime errors. The purpose of using assertions is to help developers locate the root cause of potential bugs more quickly. Assertions will never throw an error unless there is actually a bug in the program.

Defensive Programming and the unittest Unit Test Library

Assertions are generally used in defensive programming, runtime program logic checking, contract verification, program constant validation, and document inspection; there is no need to add assertions to code that will never fail. We frequently use assertions in unit test frameworks. A unit test refers to the inspection and verification of the smallest testable unit in software, which is usually a function or a method. Unit tests help us detect errors early and improve the reliability and maintainability of code. Python has a built-in unittest unit test library that can be used to write test code.The usage of several commonly used assertions in unittest is as follows:

Name

Function

assertEqual

If the two values are equal, then pass

assertNotEqual

If the two values are not equal, then pass

assertTrue

If the bool value is determined to be True, then execute pass

assertFalse

If the bool value is determined to be False, then Pass

assertIsNone

If it does not exist, then pass

assertIsNotNone

If it exists, then pass

We will cover the usage of assertions in the unittest unit test library later, so just a brief understanding here is sufficient.

When using assertions, a key point to note is that using the -o and -oo flags on the command line, or modifying the PYTHONOPTIMIZE environment variable in CPython, will globally disable assertions. In this case, all assertion statements will become invalid: the program will skip them directly without processing, and thus no conditional expressions will be executed.

In the actual development process of Python, we often encounter performance issues. The so-called performance issues refer to the fact that Python programs run relatively slowly. When encountering performance issues, we have two methods to optimize them: one is to modify the source code, optimize the logic to improve performance; the other is to enable the optimization function of the Python interpreter.

The Python interpreter provides two optimization levels:

Using the -O parameter (uppercase letter O) can achieve level 1 optimization effects, while using the -OO parameter (both are uppercase letter O) can achieve level 2 optimization effects. These optimization methods are very helpful for improving the running speed of Python programs.

python -o main.py

help () function

In Python, there are numerous built-in functions and modules, some of which may be inconspicuous but are extremely practical. One of them is the help () function. The help () function is used to obtain help information about Python objects, modules, functions or methods, which can help you better understand Python's features and how to use them correctly.

The basic usage of the help () function is extremely straightforward: simply enter the name of an object, module, function or method in the interactive Python interpreter, and pass it as an argument to the help () function.

help syntax: help([object])

Here, let's take a look at the help output of the assert function:

1
The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 29 Python Implementation of Classes and Objects – Assertions, Defensive Programming and the Usage of the help function