Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`abc` --- Abstract Base Classes |
| 3 | ==================================== |
| 4 | |
| 5 | .. module:: abc |
| 6 | :synopsis: Abstract base classes according to PEP 3119. |
| 7 | .. moduleauthor:: Guido van Rossum |
| 8 | .. sectionauthor:: Georg Brandl |
| 9 | .. much of the content adapted from docstrings |
| 10 | |
| 11 | This module provides the infrastructure for defining abstract base classes |
| 12 | (ABCs) in Python, as outlined in :pep:`3119`. |
| 13 | |
| 14 | Concrete base ABCs to derive from can be found in the :mod:`collections` module. |
| 15 | |
| 16 | |
| 17 | The module provides the following class: |
| 18 | |
| 19 | .. class:: ABCMeta |
| 20 | |
| 21 | Metaclass for defining Abstract Base Classes (ABCs). |
| 22 | |
| 23 | Use this metaclass to create an ABC. An ABC can be subclassed directly, and |
| 24 | then acts as a mix-in class. You can also register unrelated concrete |
| 25 | classes (even built-in classes) and unrelated ABCs as "virtual subclasses" -- |
| 26 | these and their descendants will be considered subclasses of the registering |
| 27 | ABC by the built-in :func:`issubclass` function, but the registering ABC |
| 28 | won't show up in their MRO (Method Resolution Order) nor will method |
| 29 | implementations defined by the registering ABC be callable (not even via |
| 30 | :func:`super`). |
| 31 | |
| 32 | Classes created with a metaclass of :class:`ABCMeta` have the following method: |
| 33 | |
| 34 | .. method:: register(subclass) |
| 35 | |
| 36 | Register *subclass* as a "virtual subclass" of this ABC. From now on, |
| 37 | ``issubclass(subclass, ABC)`` is true. |
| 38 | |
| 39 | |
| 40 | You can also override this method in an abstract base class: |
| 41 | |
| 42 | .. method:: __subclasshook__(subclass) |
| 43 | |
| 44 | (Must be defined as a class method.) |
| 45 | |
| 46 | Check whether *subclass* is considered a subclass of this ABC. This means |
| 47 | that you can customize the behavior of ``issubclass`` further without the |
| 48 | need to call :meth:`register` on every class you want to consider a |
| 49 | subclass of the ABC. |
| 50 | |
| 51 | This method should return ``True``, ``False`` or ``NotImplemented``. If |
| 52 | it returns ``True``, the *subclass* is considered a subclass of this ABC. |
| 53 | If it returns ``False``, the *subclass* is not considered a subclass of |
| 54 | this ABC, even if it would normally be one. If it returns |
| 55 | ``NotImplemented``, the subclass check is continued with the usual |
| 56 | mechanism. |
| 57 | |
| 58 | |
| 59 | To demonstrate these concepts, look at this example ABC definition:: |
| 60 | |
| 61 | class MyIterator: |
| 62 | pass |
| 63 | |
| 64 | class Iterator(metaclass=ABCMeta): |
| 65 | |
| 66 | @abstractmethod |
| 67 | def __next__(self): |
| 68 | raise StopIteration |
| 69 | |
| 70 | def __iter__(self): |
| 71 | return self |
| 72 | |
| 73 | @classmethod |
| 74 | def __subclasshook__(cls, C): |
| 75 | if cls is Iterator: |
| 76 | if any("__next__" in B.__dict__ for B in C.__mro__): |
| 77 | return True |
| 78 | return NotImplemented |
| 79 | |
| 80 | Iterator.register(MyIterator) |
| 81 | |
| 82 | The ABC ``Iterator`` defines the two standard iterator methods: |
| 83 | :meth:`__iter__` and :meth:`__next__`. The :meth:`__iter__` method is given |
| 84 | a default implementation, while the :meth:`__next__` method is abstract. |
| 85 | |
| 86 | .. XXX why is an implementation given then? |
| 87 | |
| 88 | The :meth:`__subclasshook__` class method defined here says that any class |
| 89 | that has a :meth:`__next__` method in its :attr:`__dict__` (or in that of one |
| 90 | of its subclasses, accessed via the :attr:`__mro__`) is considered an |
| 91 | ``Iterator`` too. |
| 92 | |
| 93 | Finally, the last line makes ``MyIterator`` a virtual subclass of |
| 94 | ``Iterator``, even though it does not define a :meth:`__next__` method. |
| 95 | (Of course, this doesn't make much sense in this context.) |
| 96 | |
| 97 | .. XXX perhaps find better example |
| 98 | |
| 99 | |
| 100 | It also provides the following decorators: |
| 101 | |
| 102 | .. function:: abstractmethod(function) |
| 103 | |
| 104 | A decorator indicating abstract methods. |
| 105 | |
| 106 | Requires that the metaclass is :class:`ABCMeta` or derived from it. A class |
| 107 | that has a metaclass derived from :class:`ABCMeta` cannot be instantiated |
| 108 | unless all of its abstract methods are overridden. The abstract methods can |
| 109 | be called using any of the the normal 'super' call mechanisms. |
| 110 | |
| 111 | Usage:: |
| 112 | |
| 113 | class C(metaclass=ABCMeta): |
| 114 | @abstractmethod |
| 115 | def my_abstract_method(self, ...): |
| 116 | ... |
| 117 | |
| 118 | |
| 119 | .. function:: abstractproperty(property) |
| 120 | |
| 121 | A decorator indicating abstract properties. |
| 122 | |
| 123 | Requires that the metaclass is :class:`ABCMeta` or derived from it. A class |
| 124 | that has a metaclass derived from :class:`ABCMeta` cannot be instantiated |
| 125 | unless all of its abstract properties are overridden. The abstract |
| 126 | properties can be called using any of the the normal 'super' call mechanisms. |
| 127 | |
| 128 | Usage:: |
| 129 | |
| 130 | class C(metaclass=ABCMeta): |
| 131 | @abstractproperty |
| 132 | def my_abstract_property(self): |
| 133 | ... |
| 134 | |
| 135 | This defines a read-only property; you can also define a read-write abstract |
| 136 | property using the 'long' form of property declaration:: |
| 137 | |
| 138 | class C(metaclass=ABCMeta): |
| 139 | def getx(self): ... |
| 140 | def setx(self, value): ... |
| 141 | x = abstractproperty(getx, setx) |
| 142 | |