Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Interface-oriented programming in Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "case analysis of interface-oriented programming in Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "interface-oriented programming case analysis in Python".

Preface

Interfaces play an important role in software engineering, and with the continuous expansion of the functions of applications, it is difficult to manage the updates and changes of the code base. In many cases, you will find that there are classes that look very similar but are not related, which can make some difficult to maintain. In this sharing, you will see how you can use the Python interface to help determine.

We mainly understand the content from the following aspects:

Understand how interfaces work and considerations for creating Python interfaces

Understand the importance of interfaces in dynamic languages like Python

Implement an informal Python interface

Use abc.ABCMeta and @ abc.abstractmethod to implement a formal Python interface

Interfaces in Python are handled differently from most other languages, and their design complexity is also different. By the end of this tutorial, you will have a better understanding of some aspects of Python's data model and how interfaces in Python compare to interfaces in languages such as Java, C++, and Go.

Overview of Python Interfac

At a high level, the interface acts as the blueprint for designing the class, and defining methods in the interface is no different from defining it in the class. However, unlike classes, these methods are abstract methods. An abstract method is a simple way to define an interface. Methods are defined here, and there is no rush to implement them. This is done by the concrete class, which then implements the interface, giving concrete meaning to the abstract methods of the interface.

Compared with languages such as Java, Go and C++, the interface design method of Python is somewhat different. Each of these languages provides an interface keyword to define the interface, which is not provided in Python. Python is obviously different from other languages in another respect. Python does not require classes that implement the interface to define all abstract methods of the interface

Informal interface

In some cases, a formal Python interface may not be required to strictly standardize. The dynamic nature of Python allows you to implement an informal interface. The informal Python interface is a method that defines a method that can be overloaded.

In the following example, you will start from the perspective of a data engineer who needs to extract text from a variety of different unstructured file types, such as PDF and email. An informal interface is created that defines methods in PdfParser and EmlParser concrete classes.

Class InformalParserInterface: def load_data_source (self, path: str, file_name: str)-> str: "Load in the file for extracting text. Pass def extract_text (self, full_file_name: str)-> dict: "Extract text from the currently loaded file." Pass

Two methods are defined in the InformalParserInterface class, .load _ data_source () and .extract _ text (). Although the method is defined, it is not implemented. Next we will need to implement these two methods to create a class that inherits from InformalParserInterface. We are concerned that the interface defines a general process for extracting text, which can also be seen as a specification, that is, we first load the data source, and then extract the text on the data source.

InformalParserInterface looks like a standard python class. However, because it looks like an interface, you can think of this class as an interface.

You define two classes that implement InformalParserInterface. To use the interface, first create a concrete class to inherit from the. The interface, that is, this class is a subclass of the interface class, provides a concrete implementation of the interface abstract methods. Two concrete classes will be created to implement your interface. The first is PdfParser, which will be used to parse the text of the PDF file.

Class PdfParser (InformalParserInterface): "" Extract text from a PDF "def load_data_source (self, path: str, file_name: str)-> str:" Overrides InformalParserInterface.load_data_source () "pass def extract_text (self, full_file_path: str)-> dict:"Overrides InformalParserInterface.extract_text ()" pass "

The implementation of InformalParserInterface now allows you to extract text from PDF files. The second concrete class is EmlParser, which will be used to parse the text in the e-mail.

Class EmlParser (InformalParserInterface): "Extract text from an email"def load_data_source (self, path: str, file_name: str)-> str:" Overrides InformalParserInterface.load_data_source () "pass def extract_text_from_email (self, full_file_path: str)-> dict:" A method defined only in EmlParser. Does not override InformalParserInterface.extract_text () "pass"

The implementation of InformalParserInterface now allows you to extract text from e-mail files.

So far, two concrete implementations of InformalPythonInterface have been defined. Note, however, that EmlParser does not correctly define .extract _ text (). To check whether EmlParser implements the InformalParserInterface abstract method, that is, the interface method, refer to the following code.

> # Check if both PdfParser and EmlParser implement InformalParserInterface > issubclass (PdfParser, InformalParserInterface) True > issubclass (EmlParser, InformalParserInterface) True Thank you for your reading. The above is the content of "Interface-oriented programming instance Analysis in Python". After the study of this article, I believe you have a deeper understanding of the problem of interface-oriented programming example analysis in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report