[Python-checkins] r56647 - peps/trunk/pep-3119.txt
guido.van.rossum
python-checkins at python.org
Wed Aug 1 19:29:38 CEST 2007
Author: guido.van.rossum
Date: Wed Aug 1 19:29:38 2007
New Revision: 56647
Modified:
peps/trunk/pep-3119.txt
Log:
Add @abstractproperty. PEP 3141 needs it for .real, .imag.
Modified: peps/trunk/pep-3119.txt
==============================================================================
--- peps/trunk/pep-3119.txt (original)
+++ peps/trunk/pep-3119.txt Wed Aug 1 19:29:38 2007
@@ -237,8 +237,8 @@
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 @@
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 @@
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
---------------------------------
More information about the Python-checkins
mailing list