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