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
Abstracts:
In Python, assertion is a commonly used debugging tool that allows programmers to write a line that checks for a certain condition. This article introduces the application scenarios and features of assertions and the use of assert statements, as well as defensive programming and the help() function.
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
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
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)
Assert
In Python programming, assert is a powerful debugging tool that is widely used in the debugging and development of code. By inserting assertion statements into code, we can critically check the correctness of the code. When the condition of the assertion is false, the program will raise AssertionError exception and stop execution. When the assertion is true, the program continues as usual, as if the assertion had never existed. Therefore, it is important to use assertions appropriately to ensure the quality and stability of your 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.
Unlike anomalies, assertions are intended to alert the developer that an unrecoverable error may have occurred in the program. For some foreseeable errors, such as a failure to find a relevant file, users are usually able to correct or retry on their own.However, assertions are not made for such problems. In a bug-free program, assertions will always be silent. But if an assertion is violated, the program crashes and reports a bug, clearly indicating to the developer which "impossible" situation has occurred. This makes it much easier to track down and fix bugs in a program.In Python, assert statements are an aid to debugging; they are not a mechanism for handling runtime errors. The purpose of using assertions is to help developers find the root cause of possible bugs faster.Assertions never throw an error unless there is an actual bug in the program.
Defensive Programming with the unittest Unit Test Library
Assertions are generally used in defensive programming, checking program logic at runtime, checking conventions, program constants, checking documentation, and adding assertions unnecessarily to code that never fails to run. In a unit testing framework, we often use assertions. By unit testing, we mean checking and verifying the smallest testable unit of software, usually a function or a method. Unit testing helps us catch errors at an early stage and improves the reliability and maintainability of the code. In Python, there is a built-in unittest library that can be used to write test code. unittest uses several common assertions as follows:
name (of a thing) | corresponds English -ity, -ism, -ization |
---|---|
assertEqual | If the two values are equal, pass |
assertNotEqual | If the two values are not equal, pass |
assertTrue | If the value of bool is True, then pass |
assertFalse | Pass if the bool value is False. |
assertIsNone | does not exist, then pass |
assertIsNotNone | exists, then pass |
We'll talk about the use of assertions in the unittest unit test library later, so here's a quick overview.
A point of special note when using assertions is that they can be globally disabled by using the -o and -oo flags on the command line, or by modifying the PYTHONOPTIMIZE environment variable in CPython. In this case, all assertion statements will be disabled and the program will simply skip over them without processing the assertions, so no conditional expressions will be executed.
In the real world of Python development, we often encounter performance problems. A performance problem is when a Python program runs relatively slowly. There are two ways to optimize for performance problems: either modify the source code to optimize the logic and improve performance, or turn on the Python interpreter's optimization features.
The Python interpreter provides two levels of optimization:
- level1 optimization: optimized by setting the built-in __debug__ global variable to false and removing the assert statement.
- level2 optimization: In addition to the level1 optimizations, the documentation of the function (i.e., the '''' comment after the function) is removed.
You can use the -O argument to achieve a level1 optimization (capital O), and the -OO argument to achieve a level2 optimization (both capital O's). These optimizations can be very helpful in speeding up Python programs.
python -o
help() function
In Python, there are a number of built-in functions and modules, some of which may be unobtrusive but are very useful. One of them is the help() function, which is used to get help information about a Python object, module, function, or method. It allows you to better understand what Python does and how to use it properly.
The basic usage of the help() function is as simple as typing the name of an object, module, function, or method into the interactive Python interpreter and passing it as an argument to the help() function.
help syntax: help([object])
Here, let's look at the help output of the assert function: