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. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Guido van Rossum |
| 8 | .. sectionauthor:: Georg Brandl |
| 9 | .. much of the content adapted from docstrings |
| 10 | |
Raymond Hettinger | ead4975 | 2011-01-24 16:28:06 +0000 | [diff] [blame] | 11 | **Source code:** :source:`Lib/abc.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Éric Araujo | 8ddf7c2 | 2011-06-15 17:49:20 +0200 | [diff] [blame] | 15 | This module provides the infrastructure for defining :term:`abstract base |
Andrew Svetlov | b67596d | 2012-12-13 19:09:33 +0200 | [diff] [blame] | 16 | classes <abstract base class>` (ABCs) in Python, as outlined in :pep:`3119`; |
| 17 | see the PEP for why this was added to Python. (See also :pep:`3141` and the |
| 18 | :mod:`numbers` module regarding a type hierarchy for numbers based on ABCs.) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 19 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 20 | The :mod:`collections` module has some concrete classes that derive from |
| 21 | ABCs; these can, of course, be further derived. In addition the |
Éric Araujo | 3622680 | 2011-06-03 20:49:39 +0200 | [diff] [blame] | 22 | :mod:`collections.abc` submodule has some ABCs that can be used to test whether |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 23 | a class or instance provides a particular interface, for example, is it |
| 24 | hashable or a mapping. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 25 | |
| 26 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 27 | This module provides the metaclass :class:`ABCMeta` for defining ABCs and |
| 28 | a helper class :class:`ABC` to alternatively define ABCs through inheritance: |
| 29 | |
| 30 | .. class:: ABC |
| 31 | |
| 32 | A helper class that has :class:`ABCMeta` as its metaclass. With this class, |
| 33 | an abstract base class can be created by simply deriving from :class:`ABC` |
| 34 | avoiding sometimes confusing metaclass usage, for example:: |
| 35 | |
| 36 | from abc import ABC |
| 37 | |
| 38 | class MyABC(ABC): |
| 39 | pass |
| 40 | |
| 41 | Note that the type of :class:`ABC` is still :class:`ABCMeta`, therefore |
| 42 | inheriting from :class:`ABC` requires the usual precautions regarding |
| 43 | metaclass usage, as multiple inheritance may lead to metaclass conflicts. |
| 44 | One may also define an abstract base class by passing the metaclass |
| 45 | keyword and using :class:`ABCMeta` directly, for example:: |
| 46 | |
| 47 | from abc import ABCMeta |
| 48 | |
| 49 | class MyABC(metaclass=ABCMeta): |
| 50 | pass |
| 51 | |
| 52 | .. versionadded:: 3.4 |
| 53 | |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 54 | |
| 55 | .. class:: ABCMeta |
| 56 | |
| 57 | Metaclass for defining Abstract Base Classes (ABCs). |
| 58 | |
| 59 | Use this metaclass to create an ABC. An ABC can be subclassed directly, and |
| 60 | then acts as a mix-in class. You can also register unrelated concrete |
| 61 | classes (even built-in classes) and unrelated ABCs as "virtual subclasses" -- |
| 62 | these and their descendants will be considered subclasses of the registering |
| 63 | ABC by the built-in :func:`issubclass` function, but the registering ABC |
| 64 | won't show up in their MRO (Method Resolution Order) nor will method |
| 65 | implementations defined by the registering ABC be callable (not even via |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 66 | :func:`super`). [#]_ |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 67 | |
| 68 | Classes created with a metaclass of :class:`ABCMeta` have the following method: |
| 69 | |
| 70 | .. method:: register(subclass) |
| 71 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 72 | Register *subclass* as a "virtual subclass" of this ABC. For |
| 73 | example:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 74 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 75 | from abc import ABC |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 76 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 77 | class MyABC(ABC): |
| 78 | pass |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 79 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 80 | MyABC.register(tuple) |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 81 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 82 | assert issubclass(tuple, MyABC) |
| 83 | assert isinstance((), MyABC) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 84 | |
Éric Araujo | 6c3787c | 2011-02-24 18:03:10 +0000 | [diff] [blame] | 85 | .. versionchanged:: 3.3 |
| 86 | Returns the registered subclass, to allow usage as a class decorator. |
| 87 | |
Łukasz Langa | eadd8cf | 2013-05-25 18:41:50 +0200 | [diff] [blame] | 88 | .. versionchanged:: 3.4 |
| 89 | To detect calls to :meth:`register`, you can use the |
| 90 | :func:`get_cache_token` function. |
| 91 | |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 92 | You can also override this method in an abstract base class: |
| 93 | |
| 94 | .. method:: __subclasshook__(subclass) |
| 95 | |
| 96 | (Must be defined as a class method.) |
| 97 | |
| 98 | Check whether *subclass* is considered a subclass of this ABC. This means |
| 99 | that you can customize the behavior of ``issubclass`` further without the |
| 100 | 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] | 101 | subclass of the ABC. (This class method is called from the |
| 102 | :meth:`__subclasscheck__` method of the ABC.) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 103 | |
| 104 | This method should return ``True``, ``False`` or ``NotImplemented``. If |
| 105 | it returns ``True``, the *subclass* is considered a subclass of this ABC. |
| 106 | If it returns ``False``, the *subclass* is not considered a subclass of |
| 107 | this ABC, even if it would normally be one. If it returns |
| 108 | ``NotImplemented``, the subclass check is continued with the usual |
| 109 | mechanism. |
| 110 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 111 | .. XXX explain the "usual mechanism" |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 112 | |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 113 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 114 | For a demonstration of these concepts, look at this example ABC definition:: |
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 | class Foo: |
| 117 | def __getitem__(self, index): |
| 118 | ... |
| 119 | def __len__(self): |
| 120 | ... |
| 121 | def get_iterator(self): |
| 122 | return iter(self) |
| 123 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 124 | class MyIterable(ABC): |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 125 | |
| 126 | @abstractmethod |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 127 | def __iter__(self): |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 128 | while False: |
| 129 | yield None |
| 130 | |
| 131 | def get_iterator(self): |
| 132 | return self.__iter__() |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 133 | |
| 134 | @classmethod |
| 135 | def __subclasshook__(cls, C): |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 136 | if cls is MyIterable: |
| 137 | if any("__iter__" in B.__dict__ for B in C.__mro__): |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 138 | return True |
| 139 | return NotImplemented |
| 140 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 141 | MyIterable.register(Foo) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 142 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 143 | The ABC ``MyIterable`` defines the standard iterable method, |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 144 | :meth:`~iterator.__iter__`, as an abstract method. The implementation given |
| 145 | here can still be called from subclasses. The :meth:`get_iterator` method |
| 146 | is also part of the ``MyIterable`` abstract base class, but it does not have |
| 147 | to be overridden in non-abstract derived classes. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 148 | |
| 149 | The :meth:`__subclasshook__` class method defined here says that any class |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 150 | that has an :meth:`~iterator.__iter__` method in its |
| 151 | :attr:`~object.__dict__` (or in that of one of its base classes, accessed |
| 152 | via the :attr:`~class.__mro__` list) is considered a ``MyIterable`` too. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 153 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 154 | Finally, the last line makes ``Foo`` a virtual subclass of ``MyIterable``, |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 155 | even though it does not define an :meth:`~iterator.__iter__` method (it uses |
| 156 | the old-style iterable protocol, defined in terms of :meth:`__len__` and |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 157 | :meth:`__getitem__`). Note that this will not make ``get_iterator`` |
| 158 | available as a method of ``Foo``, so it is provided separately. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 159 | |
| 160 | |
Georg Brandl | 09bc642 | 2012-12-16 13:32:33 +0100 | [diff] [blame] | 161 | |
Andrew Svetlov | b67596d | 2012-12-13 19:09:33 +0200 | [diff] [blame] | 162 | |
Harshul jain | 52c6b89 | 2018-02-21 10:00:01 +0530 | [diff] [blame] | 163 | The :mod:`abc` module also provides the following decorator: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 164 | |
Éric Araujo | 9bc9ab5 | 2012-12-08 18:35:31 -0500 | [diff] [blame] | 165 | .. decorator:: abstractmethod |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 166 | |
| 167 | A decorator indicating abstract methods. |
| 168 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 169 | Using this decorator requires that the class's metaclass is :class:`ABCMeta` |
| 170 | or is derived from it. A class that has a metaclass derived from |
| 171 | :class:`ABCMeta` cannot be instantiated unless all of its abstract methods |
| 172 | and properties are overridden. The abstract methods can be called using any |
| 173 | of the normal 'super' call mechanisms. :func:`abstractmethod` may be used |
| 174 | to declare abstract methods for properties and descriptors. |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 175 | |
| 176 | Dynamically adding abstract methods to a class, or attempting to modify the |
| 177 | abstraction status of a method or class once it is created, are not |
| 178 | supported. The :func:`abstractmethod` only affects subclasses derived using |
| 179 | regular inheritance; "virtual subclasses" registered with the ABC's |
| 180 | :meth:`register` method are not affected. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 181 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 182 | When :func:`abstractmethod` is applied in combination with other method |
| 183 | descriptors, it should be applied as the innermost decorator, as shown in |
| 184 | the following usage examples:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 185 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 186 | class C(ABC): |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 187 | @abstractmethod |
| 188 | def my_abstract_method(self, ...): |
| 189 | ... |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 190 | @classmethod |
| 191 | @abstractmethod |
| 192 | def my_abstract_classmethod(cls, ...): |
| 193 | ... |
| 194 | @staticmethod |
| 195 | @abstractmethod |
| 196 | def my_abstract_staticmethod(...): |
| 197 | ... |
| 198 | |
| 199 | @property |
| 200 | @abstractmethod |
| 201 | def my_abstract_property(self): |
| 202 | ... |
| 203 | @my_abstract_property.setter |
| 204 | @abstractmethod |
| 205 | def my_abstract_property(self, val): |
| 206 | ... |
| 207 | |
| 208 | @abstractmethod |
| 209 | def _get_x(self): |
| 210 | ... |
| 211 | @abstractmethod |
| 212 | def _set_x(self, val): |
| 213 | ... |
| 214 | x = property(_get_x, _set_x) |
| 215 | |
| 216 | In order to correctly interoperate with the abstract base class machinery, |
| 217 | the descriptor must identify itself as abstract using |
| 218 | :attr:`__isabstractmethod__`. In general, this attribute should be ``True`` |
| 219 | if any of the methods used to compose the descriptor are abstract. For |
| 220 | example, Python's built-in property does the equivalent of:: |
| 221 | |
| 222 | class Descriptor: |
| 223 | ... |
| 224 | @property |
| 225 | def __isabstractmethod__(self): |
| 226 | return any(getattr(f, '__isabstractmethod__', False) for |
| 227 | f in (self._fget, self._fset, self._fdel)) |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 228 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 229 | .. note:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 230 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 231 | Unlike Java abstract methods, these abstract |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 232 | methods may have an implementation. This implementation can be |
| 233 | called via the :func:`super` mechanism from the class that |
| 234 | overrides it. This could be useful as an end-point for a |
| 235 | super-call in a framework that uses cooperative |
| 236 | multiple-inheritance. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 237 | |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 238 | |
Harshul jain | 52c6b89 | 2018-02-21 10:00:01 +0530 | [diff] [blame] | 239 | The :mod:`abc` module also supports the following legacy decorators: |
| 240 | |
Éric Araujo | 9bc9ab5 | 2012-12-08 18:35:31 -0500 | [diff] [blame] | 241 | .. decorator:: abstractclassmethod |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 242 | |
Harshul jain | 52c6b89 | 2018-02-21 10:00:01 +0530 | [diff] [blame] | 243 | .. versionadded:: 3.2 |
| 244 | .. deprecated:: 3.3 |
| 245 | It is now possible to use :class:`classmethod` with |
| 246 | :func:`abstractmethod`, making this decorator redundant. |
| 247 | |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 248 | A subclass of the built-in :func:`classmethod`, indicating an abstract |
| 249 | classmethod. Otherwise it is similar to :func:`abstractmethod`. |
| 250 | |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 251 | This special case is deprecated, as the :func:`classmethod` decorator |
| 252 | is now correctly identified as abstract when applied to an abstract |
| 253 | method:: |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 254 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 255 | class C(ABC): |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 256 | @classmethod |
| 257 | @abstractmethod |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 258 | def my_abstract_classmethod(cls, ...): |
| 259 | ... |
| 260 | |
| 261 | |
Éric Araujo | 9bc9ab5 | 2012-12-08 18:35:31 -0500 | [diff] [blame] | 262 | .. decorator:: abstractstaticmethod |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 263 | |
Harshul jain | 52c6b89 | 2018-02-21 10:00:01 +0530 | [diff] [blame] | 264 | .. versionadded:: 3.2 |
| 265 | .. deprecated:: 3.3 |
| 266 | It is now possible to use :class:`staticmethod` with |
| 267 | :func:`abstractmethod`, making this decorator redundant. |
| 268 | |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 269 | A subclass of the built-in :func:`staticmethod`, indicating an abstract |
| 270 | staticmethod. Otherwise it is similar to :func:`abstractmethod`. |
| 271 | |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 272 | This special case is deprecated, as the :func:`staticmethod` decorator |
| 273 | is now correctly identified as abstract when applied to an abstract |
| 274 | method:: |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 275 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 276 | class C(ABC): |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 277 | @staticmethod |
| 278 | @abstractmethod |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 279 | def my_abstract_staticmethod(...): |
| 280 | ... |
| 281 | |
| 282 | |
Daisuke Miyakawa | 0e61e67 | 2017-10-12 23:39:43 +0900 | [diff] [blame] | 283 | .. decorator:: abstractproperty |
Georg Brandl | 2d14098 | 2007-09-04 15:45:25 +0000 | [diff] [blame] | 284 | |
Harshul jain | 52c6b89 | 2018-02-21 10:00:01 +0530 | [diff] [blame] | 285 | .. deprecated:: 3.3 |
| 286 | It is now possible to use :class:`property`, :meth:`property.getter`, |
| 287 | :meth:`property.setter` and :meth:`property.deleter` with |
| 288 | :func:`abstractmethod`, making this decorator redundant. |
| 289 | |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 290 | A subclass of the built-in :func:`property`, indicating an abstract |
| 291 | property. |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 292 | |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 293 | This special case is deprecated, as the :func:`property` decorator |
| 294 | is now correctly identified as abstract when applied to an abstract |
| 295 | method:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 296 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 297 | class C(ABC): |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 298 | @property |
| 299 | @abstractmethod |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 300 | def my_abstract_property(self): |
| 301 | ... |
| 302 | |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 303 | The above example defines a read-only property; you can also define a |
| 304 | read-write abstract property by appropriately marking one or more of the |
| 305 | underlying methods as abstract:: |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 306 | |
Eric Appelt | 122e88a | 2017-08-30 17:47:52 -0500 | [diff] [blame] | 307 | class C(ABC): |
Nick Coghlan | cea27be | 2012-12-08 22:56:02 +1000 | [diff] [blame] | 308 | @property |
| 309 | def x(self): |
| 310 | ... |
| 311 | |
| 312 | @x.setter |
| 313 | @abstractmethod |
| 314 | def x(self, val): |
| 315 | ... |
| 316 | |
| 317 | If only some components are abstract, only those components need to be |
| 318 | updated to create a concrete property in a subclass:: |
| 319 | |
| 320 | class D(C): |
| 321 | @C.x.setter |
| 322 | def x(self, val): |
| 323 | ... |
| 324 | |
Georg Brandl | aeaa546 | 2007-09-04 08:11:03 +0000 | [diff] [blame] | 325 | |
Łukasz Langa | eadd8cf | 2013-05-25 18:41:50 +0200 | [diff] [blame] | 326 | The :mod:`abc` module also provides the following functions: |
| 327 | |
| 328 | .. function:: get_cache_token() |
| 329 | |
| 330 | Returns the current abstract base class cache token. |
| 331 | |
R David Murray | 3edcc78 | 2013-12-24 16:13:32 -0500 | [diff] [blame] | 332 | The token is an opaque object (that supports equality testing) identifying |
| 333 | the current version of the abstract base class cache for virtual subclasses. |
| 334 | The token changes with every call to :meth:`ABCMeta.register` on any ABC. |
Łukasz Langa | eadd8cf | 2013-05-25 18:41:50 +0200 | [diff] [blame] | 335 | |
| 336 | .. versionadded:: 3.4 |
| 337 | |
| 338 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 339 | .. rubric:: Footnotes |
| 340 | |
| 341 | .. [#] C++ programmers should note that Python's virtual base class |
| 342 | concept is not the same as C++'s. |