Please enable JavaScript.
Coggle requires JavaScript to display documents.
factory method python codes, Implementing an Interface in Python,…
-
-
Informal Interfaces
Python’s dynamic nature allows you to implement an informal interface. An informal Python interface is a class that defines methods that can be overridden, but there’s no strict enforcement.
-
Ideally, you would want issubclass(EmlParser, InformalParserInterface) to return False when the implementing class doesn’t define all of the interface’s abstract methods.
To do this, you’ll create a metaclass called ParserMeta. You’ll be overriding two dunder methods:
-
Formal Interfaces
Informal interfaces can be useful for projects with a small code base and a limited number of programmers. However, informal interfaces would be the wrong approach for larger applications.
-
-
Using abc.ABCMeta
Rather than create your own metaclass, you’ll use abc.ABCMeta as the metaclass.
-
ou’ll overwrite .subclasshook() in place of .instancecheck() and .subclasscheck(), as it creates a more reliable implementation of these dunder methods.
-
-
-
Conclusion
Python offers great flexibility when you’re creating interfaces. An informal Python interface is useful for small projects where you’re less likely to get confused as to what the return types of the methods are. As a project grows, the need for a formal Python interface becomes more important as it becomes more difficult to infer return types. This ensures that the concrete class, which implements the interface, overwrites the abstract methods.
-
-
-