Add @abstractproperty. PEP 3141 needs it for .real, .imag.
This commit is contained in:
parent
575089c32c
commit
8f149af2f9
39
pep-3119.txt
39
pep-3119.txt
|
@ -237,8 +237,8 @@ The ``abc`` Module: an ABC Support Framework
|
|||
|
||||
The new standard library module ``abc``, written in pure Python,
|
||||
serves as an ABC support framework. It defines a metaclass
|
||||
``ABCMeta`` and a decorator ``@abstractmethod``. A sample
|
||||
implementation is given by [13]_.
|
||||
``ABCMeta`` and decorators ``@abstractmethod`` and
|
||||
``@abstractproperty``. A sample implementation is given by [13]_.
|
||||
|
||||
The ``ABCMeta`` class overrides ``__instancecheck__`` and
|
||||
``__subclasscheck__`` and defines a ``register`` method. The
|
||||
|
@ -340,11 +340,6 @@ it is created, are not supported. The ``@abstractmethod`` only
|
|||
affects subclasses derived using regular inheritance; "virtual
|
||||
subclasses" registered with the ``register()`` method are not affected.
|
||||
|
||||
It has been suggested that we should also provide a way to define
|
||||
abstract data attributes. As it is easy to add these in a later
|
||||
stage, and as the use case is considerably less common (apart from
|
||||
pure documentation), we punt on this for now.
|
||||
|
||||
**Implementation:** The ``@abstractmethod`` decorator sets the
|
||||
function attribute ``__isabstractmethod__`` to the value ``True``.
|
||||
The ``ABCMeta.__new__`` method computes the type attribute
|
||||
|
@ -366,6 +361,36 @@ may have an implementation. This implementation can be called via the
|
|||
useful as an end-point for a super-call in framework using a
|
||||
cooperative multiple-inheritance [7]_, [8]_.
|
||||
|
||||
A second decorator, ``@abstractproperty``, is defined in order to
|
||||
define abstract data attributes. Its implementation is a subclass of
|
||||
the built-in ``property`` class that adds an ``__isabstractmethod__``
|
||||
attribute::
|
||||
|
||||
class abstractproperty(property):
|
||||
__isabstractmethod__ = True
|
||||
|
||||
It can be used in two ways::
|
||||
|
||||
class C(metaclass=ABCMeta):
|
||||
|
||||
# A read-only property:
|
||||
|
||||
@abstractproperty
|
||||
def readonly(self):
|
||||
return self.__x
|
||||
|
||||
# A read-write property (cannot use decorator syntax):
|
||||
|
||||
def getx(self):
|
||||
return self.__x
|
||||
def setx(self, value):
|
||||
self.__x = value
|
||||
x = abstractproperty(getx, setx)
|
||||
|
||||
A subclass inheriting an abstract property (declared using either the
|
||||
decorator syntax or the longer form) cannot be instantiated unless it
|
||||
overrides that abstract property with a concrete property.
|
||||
|
||||
|
||||
ABCs for Containers and Iterators
|
||||
---------------------------------
|
||||
|
|
Loading…
Reference in New Issue