Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(15)

Side by Side Diff: Doc/library/abc.rst

Issue 11610: Improving property to accept abstract methods
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Doc/whatsnew/3.3.rst » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 :mod:`abc` --- Abstract Base Classes 1 :mod:`abc` --- Abstract Base Classes
2 ==================================== 2 ====================================
3 3
4 .. module:: abc 4 .. module:: abc
5 :synopsis: Abstract base classes according to PEP 3119. 5 :synopsis: Abstract base classes according to PEP 3119.
6 .. moduleauthor:: Guido van Rossum 6 .. moduleauthor:: Guido van Rossum
7 .. sectionauthor:: Georg Brandl 7 .. sectionauthor:: Georg Brandl
8 .. much of the content adapted from docstrings 8 .. much of the content adapted from docstrings
9 9
10 **Source code:** :source:`Lib/abc.py` 10 **Source code:** :source:`Lib/abc.py`
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 :meth:`__getitem__`). Note that this will not make ``get_iterator`` 126 :meth:`__getitem__`). Note that this will not make ``get_iterator``
127 available as a method of ``Foo``, so it is provided separately. 127 available as a method of ``Foo``, so it is provided separately.
128 128
129 129
130 It also provides the following decorators: 130 It also provides the following decorators:
131 131
132 .. decorator:: abstractmethod(function) 132 .. decorator:: abstractmethod(function)
133 133
134 A decorator indicating abstract methods. 134 A decorator indicating abstract methods.
135 135
136 Using this decorator requires that the class's metaclass is :class:`ABCMeta` or 136 Using this decorator requires that the class's metaclass is :class:`ABCMeta`
137 is derived from it. 137 or is derived from it. A class that has a metaclass derived from
138 A class that has a metaclass derived from :class:`ABCMeta` 138 :class:`ABCMeta` cannot be instantiated unless all of its abstract methods
139 cannot be instantiated unless all of its abstract methods and 139 and properties are overridden. The abstract methods can be called using any
140 properties are overridden. 140 of the normal 'super' call mechanisms. :func:`abstractmethod` may be used
141 The abstract methods can be called using any of the normal 'super' call 141 to declare abstract methods for properties and descriptors.
142 mechanisms.
143 142
144 Dynamically adding abstract methods to a class, or attempting to modify the 143 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 144 abstraction status of a method or class once it is created, are not
146 supported. The :func:`abstractmethod` only affects subclasses derived using 145 supported. The :func:`abstractmethod` only affects subclasses derived using
147 regular inheritance; "virtual subclasses" registered with the ABC's 146 regular inheritance; "virtual subclasses" registered with the ABC's
148 :meth:`register` method are not affected. 147 :meth:`register` method are not affected.
149 148
150 Usage:: 149 Usage::
151 150
152 class C(metaclass=ABCMeta): 151 class C(metaclass=ABCMeta):
153 @abstractmethod 152 @abstractmethod
154 def my_abstract_method(self, ...): 153 def my_abstract_method(self, ...):
155 ... 154 ...
155
156 @property
157 @abstractmethod
158 def my_abstract_property(self):
159 ...
160 @my_abstract_property.setter
161 @abstractmethod
162 def my_abstract_property(self, val):
163 ...
164
165 @abstractmethod
166 def _get_x(self):
167 ...
168 @abstractmethod
169 def _set_x(self, val):
170 ...
171 x = property(_get_x, _set_x)
172
156 173
157 .. note:: 174 .. note::
158 175
159 Unlike Java abstract methods, these abstract 176 Unlike Java abstract methods, these abstract
160 methods may have an implementation. This implementation can be 177 methods may have an implementation. This implementation can be
161 called via the :func:`super` mechanism from the class that 178 called via the :func:`super` mechanism from the class that
162 overrides it. This could be useful as an end-point for a 179 overrides it. This could be useful as an end-point for a
163 super-call in a framework that uses cooperative 180 super-call in a framework that uses cooperative
164 multiple-inheritance. 181 multiple-inheritance.
165 182
(...skipping 23 matching lines...) Expand all
189 class C(metaclass=ABCMeta): 206 class C(metaclass=ABCMeta):
190 @abstractstaticmethod 207 @abstractstaticmethod
191 def my_abstract_staticmethod(...): 208 def my_abstract_staticmethod(...):
192 ... 209 ...
193 210
194 .. versionadded:: 3.2 211 .. versionadded:: 3.2
195 212
196 213
197 .. function:: abstractproperty(fget=None, fset=None, fdel=None, doc=None) 214 .. function:: abstractproperty(fget=None, fset=None, fdel=None, doc=None)
198 215
199 A subclass of the built-in :func:`property`, indicating an abstract property. 216 .. deprecated:: 3.3
200 217 Use :class:`property` and :func:`abstractmethod` instead
201 Using this function requires that the class's metaclass is :class:`ABCMeta` o r
202 is derived from it.
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.
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 218
223 219
224 .. rubric:: Footnotes 220 .. rubric:: Footnotes
225 221
226 .. [#] C++ programmers should note that Python's virtual base class 222 .. [#] C++ programmers should note that Python's virtual base class
227 concept is not the same as C++'s. 223 concept is not the same as C++'s.
OLDNEW
« no previous file with comments | « no previous file | Doc/whatsnew/3.3.rst » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+