This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python. (See also PEP 3141 and the numbers module regarding a type hierarchy for numbers based on ABCs.)
The collections module has some concrete classes that derive from ABCs; these can, of course, be further derived. In addition the collections module has some ABCs that can be used to test whether a class or instance provides a particular interface, for example, is it hashable or a mapping.
This module provides the following class:
Classes created with a metaclass of ABCMeta have the following method:
For a demonstration of these concepts, look at this example ABC definition:
The __subclasshook__() class method defined here says that any class that has an __iter__() method in its __dict__ (or in that of one of its base classes, accessed via the __mro__ list) is considered a MyIterable too.
Finally, the line MyIterable.register(Foo) makes Foo a virtual subclass of MyIterable, even though it does not define an __iter__() method (it uses the old-style iterable protocol, defined in terms of __len__() and __getitem__()). Note that this will not make get_iterator available as a method of Foo, so it is provided separately. Also even through Foo2 doesn't get registered in MyIterable, because it has an __iter__() method, it will pass issubclass(Foo2, MyIterable) check.
From abc module, it also provides the following decorators:
abc.abstractproperty([fget[, fset[, fdel[, doc]]]])
Below are sample code:
* Python 3.1 快速導覽 - 函數 不定個數參數
* Build-in Types - Iterator Types
* The Definitive Guide on How to Use Static, Class or Abstract Methods in Python