Labs / Python OOP

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

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

Contents
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

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.

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 Across the 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 entire 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

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

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 Acquisition:

You can download the document via the following link:

https://github.com/leezisheng/Doc

1 0 74 19 1.1

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

  1. 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
  2. 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.
  3. 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.
  4. Interface Segregation Principle (ISP)
    • Client should not depend on interfaces it does not need
    • The interface should be kept single and of minimal granularity.
  5. Law of Demeter (LoD)
    • A software entity should interact with other entities as little as possible.
    • High Cohesion, Low Coupling
  6. 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

  1. 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.
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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:

  1. 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.
  2. 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
  3. Import Style
    • Each import statement occupies a separate line
    • The import order is: standard libraries, third-party libraries, custom libraries
  4. Line length limit
    • Each line shall not exceed 79 characters
    • If it exceeds the limit, you can use parentheses to wrap lines
  5. Comments and Documentation
    • Write comments and documentation in English
    • Functions are described using docstrings
  6. 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/

8

The Chinese version is available at the following link:

https://www.cnblogs.com/bymo/p/9567140.html

8
347622978 PEP8