Understanding Informal Interfaces
00:00 Informal Interfaces. At a high level, an interface acts as a blueprint for designing classes. Like classes, interfaces define methods, but unlike classes, these methods are abstract.
00:15 An abstract method is one that the interface simply defines, but it doesn’t implement. The implementation is done by classes, which implement the interface and give concrete meaning to the interface’s abstract methods. Python’s approach to interface design is somewhat different when compared to languages such as Java, Go, and C++.
00:36
Java and Go have an interface
keyword while Python doesn’t. Python deviates further from the other languages in one important aspect. It doesn’t require the class that’s implementing the interface to define all of the interface’s abstract methods.
00:52 In certain circumstances, you may not need the strict rules of a formal Python interface. Python’s dynamic nature allows you to implement an informal interface.
01:03 This interface is a class that defines methods that can be overridden, but there’s no strict enforcement. This may remind you of other areas of Python, such as its approach to typing, and along with this flexibility come some potential issues as you will see. In the first example, you’ll take the perspective of a data engineer who needs to extract text from various different unstructured file types such as PDFs and emails.
01:29 You’ll create an informal interface that defines the methods that will be in both the PDF parser and email parser concrete classes.
01:45
InformalParserInterface defines two methods, load_data_source
,
01:53
and extract_text
. These methods are defined but not implemented. The implementation will occur once you create concrete classes that inherit from InformalParserInterface.
02:05 As you can see, this class looks identical to a standard Python class. You rely on duck typing to inform users that this is an interface and it should be used accordingly.
02:18 Duck typing says that if you have an object that looks like a duck, walks like a duck and quacks like a duck, then it’s a duck. To learn more about this, check out this real Python video.
02:32 With duck typing in mind, you define two classes that implement the InformalParserInterface. To use the interface, you must create a concrete class. A concrete class is a subclass of the interface that provides an implementation of the interface’s methods.
02:48 Here you’ll create two concrete classes to implement the interface. The first is PDFParser, which you’ll use to parse the text from PDF files.
03:04
This concrete implementation now allows you to extract texts from PDF files. But note that here it consists of a pass
statement, which does nothing.
03:13 The implementation of the methods is not the point of this course, but instead, you should focus on the structure of the classes. The second concrete class is EmlParser, which you’ll use to parse the text from emails.
03:38
This concrete implementation now allows you to extract text from email files, but note that it doesn’t define extract_text
, but instead defines a different method.
03:48
extract_text_from_email
.
03:57 Let’s check on the behavior of this code in the REPL. First, all the classes are imported from informal. Next, you can check whether PDFParser and EmlParser implement InformalParserInterface.
04:22 Now this returns true for email parser, which poses a bit of a problem because it violates the definition of an interface. Now check the method resolution order (MRO) of PDFParser and EmlParser.
04:36
This tells you the superclasses of the class in question as well as the order in which they’re searched for executing a method. You can view a class’s MRO by using the __mro__
method seen on screen.
04:55 These simple informal interfaces are fine for small projects where only a few developers are working on the source code. But as projects get larger and teams grow, this could lead to developers spending a long time looking for hard to find logic errors in the code base.
05:11 So in the next section of the course, you’ll take a look at improved ways to implement informal interfaces using metaclasses and virtual base classes.
Become a Member to join the conversation.