mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Clarify example; add imports
This commit is contained in:
parent
31a0a1478b
commit
8315da4c17
1 changed files with 10 additions and 4 deletions
|
@ -1224,7 +1224,7 @@ do it where it's absolutely necessary.
|
||||||
You can write your own ABCs by using ``abc.ABCMeta`` as the
|
You can write your own ABCs by using ``abc.ABCMeta`` as the
|
||||||
metaclass in a class definition::
|
metaclass in a class definition::
|
||||||
|
|
||||||
from abc import ABCMeta
|
from abc import ABCMeta, abstractmethod
|
||||||
|
|
||||||
class Drawable():
|
class Drawable():
|
||||||
__metaclass__ = ABCMeta
|
__metaclass__ = ABCMeta
|
||||||
|
@ -1256,15 +1256,21 @@ exception for classes that don't define the method.
|
||||||
Note that the exception is only raised when you actually
|
Note that the exception is only raised when you actually
|
||||||
try to create an instance of a subclass lacking the method::
|
try to create an instance of a subclass lacking the method::
|
||||||
|
|
||||||
>>> s=Square()
|
>>> class Circle(Drawable):
|
||||||
|
... pass
|
||||||
|
...
|
||||||
|
>>> c=Circle()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "<stdin>", line 1, in <module>
|
File "<stdin>", line 1, in <module>
|
||||||
TypeError: Can't instantiate abstract class Square with abstract methods draw
|
TypeError: Can't instantiate abstract class Circle with abstract methods draw
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
Abstract data attributes can be declared using the
|
Abstract data attributes can be declared using the
|
||||||
``@abstractproperty`` decorator::
|
``@abstractproperty`` decorator::
|
||||||
|
|
||||||
|
from abc import abstractproperty
|
||||||
|
...
|
||||||
|
|
||||||
@abstractproperty
|
@abstractproperty
|
||||||
def readonly(self):
|
def readonly(self):
|
||||||
return self._x
|
return self._x
|
||||||
|
@ -3206,5 +3212,5 @@ Acknowledgements
|
||||||
|
|
||||||
The author would like to thank the following people for offering suggestions,
|
The author would like to thank the following people for offering suggestions,
|
||||||
corrections and assistance with various drafts of this article:
|
corrections and assistance with various drafts of this article:
|
||||||
Georg Brandl, Nick Coghlan, Jim Jewett, Antoine Pitrou.
|
Georg Brandl, Steve Brown, Nick Coghlan, Jim Jewett, Antoine Pitrou.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue