I did a few searches on Python3 and interfaces. I can't seem to find a direct answer on if it's pythonic, or not to use interfaces. I understand that python allows mixins, and allows duck typing. However, they get a set of "requirements" from implementing the interface. What is the decision on Python3 interfaces?
If Interfaces are pythonic then what is the best way to implement them?
While Python 2 and 3 supports this type of functionality via nominal subtyping (mixins) and abstract base classes, if you're using type annotations, PEP 544 introduces the concept of static duck typing with Protocol. Similar to Swift protocols or Java interfaces, Protocol defines an interface for subtypes (optionally including default implementations for properties and methods).
You can use Protocols now in all Python versions (except 3.5.0) by importing the typing-extensions module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With