Contents
Abstract:
This paper mainly introduces the basic definition, characteristics and application scenarios of duck typing when creating custom classes in Python, and also lists examples of "file-like object" to illustrate duck typing.
Original Link:
Previous Recommendations:
You who study embedded systems still don't know Object Oriented? ?!
More exciting content can be found here:
Speed Up Your Python: A Quick Guide to Python Parallel Computing
Understand the Debugging Principle of CM3 Microcontroller 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 use Python for Object Oriented programming, and 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, taking the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the lower computer as application examples. Meanwhile, Sourcetrail code software is used for visual reading of the code to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Main Text
Actually, for the IsSerialConnected function, any object that has the RetSerialState method can be passed in, and this polymorphism in Python is usually referred to as duck typing.

"If it walks like a duck and swims like a duck, then it's a duck". We don't care whether it actually inherits from a duck; we only need to know that it can swim or walk. Geese and swans can easily exhibit duck-like behaviors for us, which allows us to create new bird types in future designs without specifying inheritance relationships for waterfowl, and also enables designers to create classes with behaviors completely different from the original intent. For example, a future designer might create a penguin type that can walk and swim, and it also fits the same interface, even though the designer never intended to classify it as a duck.
In duck typing, the focus lies on the behaviors and methods of an object rather than its specific type or class. If an object has the required methods or properties, it can be treated as being of a specific type without the need for explicit declaration or inheritance of that type. This approach allows Python to dynamically determine an object's type at runtime, making code more flexible and concise.
Python's "file-like object" is a kind of duck typing. A real file object has a read () method that returns its content. However, many objects are regarded as "file-like object" as long as they have a read () method. Many functions accept "file-like object" as parameters, and you don't necessarily have to pass a real file object — you can completely pass any object that implements the read () method.
