The Python base classes (ABC) and the Zope interface have an overlap.
Interface
The Zope interface documentation says :
Interfaces are objects that define (the document) the external behavior of objects that "provide" them. The interface defines the behavior through:
- Unofficial documentation in the document line
- Attribute definitions
- Invariants that are conditions that must be satisfied for objects that provide an interface
Attribute definitions define specific attributes. They define an attribute name and provide documentation and attribute value restrictions. Attribute definitions can take a number of forms [...]
This blog post claims that since it supports not only class objects, but also modules and class instances, it is superior to ABC. Allegedly, one could believe that an instance of an object can be used interchangeably with a module.
I have yet to see the need for this, but you can certainly get it without checking (duck print) or, more confidently, using unittesting.
ABCs
ABC are approved in PEP 3119, which says ABCs
- The overload method
isinstance() and issubclass() . - A new abc module that serves as the "ABC support framework." It defines a metaclass for use with ABC and a decorator, which can be used to define abstract methods.
- Specific ABCs for containers and iterators to be added to the collections module.
A significant part of the thinking that the proposal does not relate to is a specific ABC mechanism, unlike with interfaces or universal functions (GF), but with clarification of philosophical questions such as “what does the set”, “what does the display”, and “what does the sequence” .
and :
ABCs are designed to solve problems that generally do not have a good solution in Python 2, such as distinguishing between mappings and sequences.
And ends with :
ABC vs. Interfaces
ABCs are not internally incompatible with the interfaces, but there is significant overlap. For now, I will leave it to the supporters. Interfaces explain why interfaces are better. I expect that most of the work, which included, for example, defining various shades of “matching,” and the nomenclature can easily be adapted to suggest using interfaces instead of ABC.
“Interfaces” in this context refer to a set of sentences for additional metadata elements bound to a class that are not part of the normal class hierarchy but allow certain types of inheritance.
Such metadata will be developed, at least in some sentences, in order to be easily modifiable by the application, allowing application developers to override the usual classification of an object.
The disadvantage of this idea of attaching mutable metadata to a class is that classes are a common state, and changing them can lead to conflicts of intent. In addition, it is necessary to redefine the classification of an object; it can be performed more cleanly using common functions: in the simplest case, you can define a general function of “category membership” which simply returns False in the base implementation and then provides redefines that return True for any interest classes .
Conclusion
It seems to me from studying the source that the Zope interface is much more complicated than ABC. It also uses several non-standard “special” methods, including __adapt__ - which it refers to the rejected PEP 246 - Object adaptation
My conclusion is that since
- ABCs are part of the standard library (and therefore more anticipated and popular),
- the language has developed built-in support for them,
issubclass and isinstance do not require import, but- I find it easier to understand and understand
I would prefer to use ABC to define interfaces.