Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 1 | :mod:`abc` --- Abstract Base Classes |
| 2 | ==================================== |
| 3 | |
| 4 | .. module:: abc |
| 5 | :synopsis: Abstract base classes according to PEP 3119. |
| 6 | .. moduleauthor:: Guido van Rossum |
| 7 | .. sectionauthor:: Georg Brandl |
| 8 | .. much of the content adapted from docstrings |
| 9 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 10 | This module provides the infrastructure for defining an :term:`abstract base |
| 11 | class` (ABCs) in Python, as outlined in :pep:`3119`; see the PEP for why this |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 12 | was added to Python. (See also :pep:`3141` and the :mod:`numbers` module |
| 13 | regarding a type hierarchy for numbers based on ABCs.) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 14 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 15 | The :mod:`collections` module has some concrete classes that derive from |
| 16 | ABCs; these can, of course, be further derived. In addition the |
| 17 | :mod:`collections` module has some ABCs that can be used to test whether |
| 18 | a class or instance provides a particular interface, for example, is it |
| 19 | hashable or a mapping. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 20 | |
| 21 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 22 | This module provides the following class: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 23 | |
| 24 | .. class:: ABCMeta |
| 25 | |
| 26 | Metaclass for defining Abstract Base Classes (ABCs). |
| 27 | |
| 28 | Use this metaclass to create an ABC. An ABC can be subclassed directly, and |
| 29 | then acts as a mix-in class. You can also register unrelated concrete |
| 30 | classes (even built-in classes) and unrelated ABCs as "virtual subclasses" -- |
| 31 | these and their descendants will be considered subclasses of the registering |
| 32 | ABC by the built-in :func:`issubclass` function, but the registering ABC |
| 33 | won't show up in their MRO (Method Resolution Order) nor will method |
| 34 | implementations defined by the registering ABC be callable (not even via |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 35 | :func:`super`). [#]_ |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 36 | |
| 37 | Classes created with a metaclass of :class:`ABCMeta` have the following method: |
| 38 | |
| 39 | .. method:: register(subclass) |
| 40 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 41 | Register *subclass* as a "virtual subclass" of this ABC. For |
| 42 | example:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 43 | |
Georg Brandl | a1c6a1c | 2009-01-03 21:26:05 +0000 | [diff] [blame] | 44 | from abc import ABCMeta |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 45 | |
Georg Brandl | a1c6a1c | 2009-01-03 21:26:05 +0000 | [diff] [blame] | 46 | class MyABC(metaclass=ABCMeta): |
| 47 | pass |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 48 | |
Georg Brandl | a1c6a1c | 2009-01-03 21:26:05 +0000 | [diff] [blame] | 49 | MyABC.register(tuple) |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 50 | |
Georg Brandl | a1c6a1c | 2009-01-03 21:26:05 +0000 | [diff] [blame] | 51 | assert issubclass(tuple, MyABC) |
| 52 | assert isinstance((), MyABC) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 53 | |
| 54 | You can also override this method in an abstract base class: |
| 55 | |
| 56 | .. method:: __subclasshook__(subclass) |
| 57 | |
| 58 | (Must be defined as a class method.) |
| 59 | |
| 60 | Check whether *subclass* is considered a subclass of this ABC. This means |
| 61 | that you can customize the behavior of ``issubclass`` further without the |
| 62 | need to call :meth:`register` on every class you want to consider a |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 63 | subclass of the ABC. (This class method is called from the |
| 64 | :meth:`__subclasscheck__` method of the ABC.) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 65 | |
| 66 | This method should return ``True``, ``False`` or ``NotImplemented``. If |
| 67 | it returns ``True``, the *subclass* is considered a subclass of this ABC. |
| 68 | If it returns ``False``, the *subclass* is not considered a subclass of |
| 69 | this ABC, even if it would normally be one. If it returns |
| 70 | ``NotImplemented``, the subclass check is continued with the usual |
| 71 | mechanism. |
| 72 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 73 | .. XXX explain the "usual mechanism" |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 74 | |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 76 | For a demonstration of these concepts, look at this example ABC definition:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 78 | class Foo: |
| 79 | def __getitem__(self, index): |
| 80 | ... |
| 81 | def __len__(self): |
| 82 | ... |
| 83 | def get_iterator(self): |
| 84 | return iter(self) |
| 85 | |
| 86 | class MyIterable(metaclass=ABCMeta): |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 87 | |
| 88 | @abstractmethod |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 89 | def __iter__(self): |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 90 | while False: |
| 91 | yield None |
| 92 | |
| 93 | def get_iterator(self): |
| 94 | return self.__iter__() |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 95 | |
| 96 | @classmethod |
| 97 | def __subclasshook__(cls, C): |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 98 | if cls is MyIterable: |
| 99 | if any("__iter__" in B.__dict__ for B in C.__mro__): |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 100 | return True |
| 101 | return NotImplemented |
| 102 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 103 | MyIterable.register(Foo) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 105 | The ABC ``MyIterable`` defines the standard iterable method, |
| 106 | :meth:`__iter__`, as an abstract method. The implementation given here can |
| 107 | still be called from subclasses. The :meth:`get_iterator` method is also |
| 108 | part of the ``MyIterable`` abstract base class, but it does not have to be |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 109 | overridden in non-abstract derived classes. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 110 | |
| 111 | The :meth:`__subclasshook__` class method defined here says that any class |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 112 | that has an :meth:`__iter__` method in its :attr:`__dict__` (or in that of |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 113 | one of its base classes, accessed via the :attr:`__mro__` list) is |
| 114 | considered a ``MyIterable`` too. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 115 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 116 | Finally, the last line makes ``Foo`` a virtual subclass of ``MyIterable``, |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 117 | even though it does not define an :meth:`__iter__` method (it uses the |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 118 | old-style iterable protocol, defined in terms of :meth:`__len__` and |
| 119 | :meth:`__getitem__`). Note that this will not make ``get_iterator`` |
| 120 | available as a method of ``Foo``, so it is provided separately. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | It also provides the following decorators: |
| 124 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 125 | .. decorator:: abstractmethod(function) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 126 | |
| 127 | A decorator indicating abstract methods. |
| 128 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 129 | Using this decorator requires that the class's metaclass is :class:`ABCMeta` or |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 130 | is derived from it. |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 131 | A class that has a metaclass derived from :class:`ABCMeta` |
| 132 | cannot be instantiated unless all of its abstract methods and |
| 133 | properties are overridden. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 134 | The abstract methods can be called using any of the normal 'super' call |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 135 | mechanisms. |
| 136 | |
| 137 | Dynamically adding abstract methods to a class, or attempting to modify the |
| 138 | abstraction status of a method or class once it is created, are not |
| 139 | supported. The :func:`abstractmethod` only affects subclasses derived using |
| 140 | regular inheritance; "virtual subclasses" registered with the ABC's |
| 141 | :meth:`register` method are not affected. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 142 | |
| 143 | Usage:: |
| 144 | |
| 145 | class C(metaclass=ABCMeta): |
| 146 | @abstractmethod |
| 147 | def my_abstract_method(self, ...): |
| 148 | ... |
| 149 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 150 | .. note:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 152 | Unlike Java abstract methods, these abstract |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 153 | methods may have an implementation. This implementation can be |
| 154 | called via the :func:`super` mechanism from the class that |
| 155 | overrides it. This could be useful as an end-point for a |
| 156 | super-call in a framework that uses cooperative |
| 157 | multiple-inheritance. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 158 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 159 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 160 | .. function:: abstractproperty(fget=None, fset=None, fdel=None, doc=None) |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 161 | |
| 162 | A subclass of the built-in :func:`property`, indicating an abstract property. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 163 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 164 | Using this function requires that the class's metaclass is :class:`ABCMeta` or |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 165 | is derived from it. |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 166 | A class that has a metaclass derived from :class:`ABCMeta` cannot be |
| 167 | instantiated unless all of its abstract methods and properties are overridden. |
| 168 | The abstract properties can be called using any of the normal |
| 169 | 'super' call mechanisms. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 170 | |
| 171 | Usage:: |
| 172 | |
| 173 | class C(metaclass=ABCMeta): |
| 174 | @abstractproperty |
| 175 | def my_abstract_property(self): |
| 176 | ... |
| 177 | |
| 178 | This defines a read-only property; you can also define a read-write abstract |
| 179 | property using the 'long' form of property declaration:: |
| 180 | |
| 181 | class C(metaclass=ABCMeta): |
| 182 | def getx(self): ... |
| 183 | def setx(self, value): ... |
| 184 | x = abstractproperty(getx, setx) |
| 185 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 186 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 187 | .. rubric:: Footnotes |
| 188 | |
| 189 | .. [#] C++ programmers should note that Python's virtual base class |
| 190 | concept is not the same as C++'s. |