Contents
- Abstract:
- Original Link:
- Previous Recommendations:
- More exciting content is available here:
- Document and Code Acquisition:
- Body Text
- Python Exception Handling Principle: "EAFP (Easier to Ask for Forgiveness than Permission)"
- The Six Principles of Software Design
- Six Programming Principles of Python
- PEP8 Style Guide

Abstract:
This paper mainly introduces the principle of Python exception handling "ask for forgiveness, not permission" when using Python for Object Oriented programming, as well as software design and Python programming principles, and also presents the PEP8 specification.
Original Link:
Previous Recommendations:
You who study embedded systems still don't know Object Oriented? ?!
More exciting content is available here:
Speed Up Your Python: A Quick Guide to Python Parallel Computing
Mastering the Debugging Principles of CM3 Microcontrollers in One Article
The "Secret Martial Arts Manual" for Computer Science Competitions
Document and Code Acquisition:
You can download the document via the following link:
https://github.com/leezisheng/Doc

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
Python Exception Handling Principle: "EAFP (Easier to Ask for Forgiveness than Permission)"
It is not difficult to find that exception handling can be fully implemented using the if. .. elif. .. else structure, so why do we still need to use exceptions? In fact, Python programmers generally adhere to the principle of "EAFP: Easier to Ask for Forgiveness than Permission", which means they execute the code first and then handle any errors that arise. The opposite approach, the principle of "LBYL: Look Before You Leap", works in exactly the reverse manner.
There are many reasons for doing this, but the main one is to avoid wasting unnecessary CPU resources on checking extremely rare scenarios. Therefore, it is wise to use exception handling for those very few exceptional cases. To elaborate further on this point, we can find that the syntax of exception handling is also highly effective in flow control. Just like if statements, exceptions can be used for decision-making, branching, and information transfer. In Python code, when dealing with potential error conditions, using an exception handling structure is easier to read and maintain compared to various conditional checks.
The Six Principles of Software Design
- Open-Closed Principle (OCP)
- Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification
- Implemented through mechanisms such as abstraction and inheritance
- Liskov Substitution Principle (Liskov Substitution Principle, LSP)
- Subclasses can transparently replace the usage of parent classes
- The parameters and return values of a subclass should be consistent with those of its parent class.
- Dependency Inversion Principle (DIP)
- High-level modules should not depend on low-level modules; both should depend on abstractions.
- Abstractions should not depend upon details; details should depend upon abstractions.
- Interface Segregation Principle (ISP)
- Client should not depend on interfaces it does not need
- The interface should be kept single and of minimal granularity.
- Law of Demeter (LoD)
- A software entity should interact with other entities as little as possible.
- High Cohesion, Low Coupling
- Single Responsibility Principle (Single Responsibility Principle, SRP)
- A class should have only one reason to change
- Responsibilities should be clearly defined to avoid duplication.
Six Programming Principles of Python
- KISS (Keep It Simple, Stupid)
- Keep the code simple and clear, and avoid overly complex designs as much as possible
- Simple and easy-to-understand code is easier to write, maintain and debug.
- DRY (Don't Repeat Yourself)
- Avoid code duplication and leverage features such as functions and modules for abstraction and encapsulation
- Reduce redundant work and improve coding efficiency
- Pythonic
- Make full use of Python's syntactic features and standard library
- Follow Python coding standards such as PEP 8 to write elegant and efficient code
- Zen of Python
- Follow the 20 design philosophy principles of Python, such as "Beautiful is better than ugly", "Explicit is better than implicit" and so on
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.
- Special cases aren't special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one-- and preferably only one --obvious way to do it.
- Although that way may not be obvious at first unless you're Dutch.
- Now is better than never.
- Although never is often better than right now.
- If the implementation is hard to explain, it's a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea -- let's do more of those!
- Python is designed to be readable, and, well, Pythonic.
- embodies the philosophy of simplicity and elegance advocated by Python
- Follow the 20 design philosophy principles of Python, such as "Beautiful is better than ugly", "Explicit is better than implicit" and so on
- Testing Is Important
- Attach importance to Test-Driven Development, and write comprehensive unit tests and integration tests
- Ensure code quality and facilitate refactoring and maintenance
- Batteries Included
- Make full use of Python's rich standard library and third-party libraries
- Avoid reinventing the wheel to improve development efficiency
PEP8 Style Guide
PEP 8 is the de facto standard code style guide for Python programming, whose main contents include:
- Naming Convention
- Variables, functions and modules use lowercase letters with underscores
- Class names use CamelCase naming convention
- Constants are all in uppercase
- Protected instance attributes start with a single underscore
- Private instance attributes start with two underscores.
- Indentation and whitespace
- Use 4 spaces as the indentation unit
- Leave two blank lines between top-level functions and class definitions
- Add a space on each side of a binary operator
- Import Style
- Each import statement occupies a separate line
- The import order is: standard libraries, third-party libraries, custom libraries
- Line length limit
- Each line shall not exceed 79 characters
- If it exceeds the limit, you can use parentheses to wrap lines
- Comments and Documentation
- Write comments and documentation in English
- Functions are described using docstrings
- Other suggestions
- Do not use tabs for indentation
- Avoid using single-letter variable names
- Use with caution
from module import *
The purpose of PEP 8 is to improve the readability and maintainability of code through a consistent coding style. Adhering to these conventions makes Python code more Pythonic.
The original text of the PEP 8 style guide is available at the following link:
https://peps.python.org/pep-0008/

The Chinese version is available at the following link:
https://www.cnblogs.com/bymo/p/9567140.html

