@@ -24,7 +24,33 @@ a class or instance provides a particular interface, for example, is it
24
24
hashable or a mapping.
25
25
26
26
27
- This module provides the following classes:
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
+
28
54
29
55
.. class :: ABCMeta
30
56
@@ -46,15 +72,15 @@ This module provides the following classes:
46
72
Register *subclass * as a "virtual subclass" of this ABC. For
47
73
example::
48
74
49
- from abc import ABCMeta
75
+ from abc import ABC
50
76
51
- class MyABC(metaclass=ABCMeta ):
52
- pass
77
+ class MyABC(ABC ):
78
+ pass
53
79
54
- MyABC.register(tuple)
80
+ MyABC.register(tuple)
55
81
56
- assert issubclass(tuple, MyABC)
57
- assert isinstance((), MyABC)
82
+ assert issubclass(tuple, MyABC)
83
+ assert isinstance((), MyABC)
58
84
59
85
.. versionchanged :: 3.3
60
86
Returns the registered subclass, to allow usage as a class decorator.
@@ -95,7 +121,7 @@ This module provides the following classes:
95
121
def get_iterator(self):
96
122
return iter(self)
97
123
98
- class MyIterable(metaclass=ABCMeta ):
124
+ class MyIterable(ABC ):
99
125
100
126
@abstractmethod
101
127
def __iter__(self):
@@ -132,17 +158,6 @@ This module provides the following classes:
132
158
available as a method of ``Foo ``, so it is provided separately.
133
159
134
160
135
- .. class :: ABC
136
-
137
- A helper class that has :class: `ABCMeta ` as its metaclass. With this class,
138
- an abstract base class can be created by simply deriving from :class: `ABC `,
139
- avoiding sometimes confusing metaclass usage.
140
-
141
- Note that the type of :class: `ABC ` is still :class: `ABCMeta `, therefore
142
- inheriting from :class: `ABC ` requires the usual precautions regarding metaclass
143
- usage, as multiple inheritance may lead to metaclass conflicts.
144
-
145
- .. versionadded :: 3.4
146
161
147
162
148
163
The :mod: `abc ` module also provides the following decorators:
@@ -168,7 +183,7 @@ The :mod:`abc` module also provides the following decorators:
168
183
descriptors, it should be applied as the innermost decorator, as shown in
169
184
the following usage examples::
170
185
171
- class C(metaclass=ABCMeta ):
186
+ class C(ABC ):
172
187
@abstractmethod
173
188
def my_abstract_method(self, ...):
174
189
...
@@ -230,7 +245,7 @@ The :mod:`abc` module also provides the following decorators:
230
245
is now correctly identified as abstract when applied to an abstract
231
246
method::
232
247
233
- class C(metaclass=ABCMeta ):
248
+ class C(ABC ):
234
249
@classmethod
235
250
@abstractmethod
236
251
def my_abstract_classmethod(cls, ...):
@@ -251,7 +266,7 @@ The :mod:`abc` module also provides the following decorators:
251
266
is now correctly identified as abstract when applied to an abstract
252
267
method::
253
268
254
- class C(metaclass=ABCMeta ):
269
+ class C(ABC ):
255
270
@staticmethod
256
271
@abstractmethod
257
272
def my_abstract_staticmethod(...):
@@ -278,7 +293,7 @@ The :mod:`abc` module also provides the following decorators:
278
293
is now correctly identified as abstract when applied to an abstract
279
294
method::
280
295
281
- class C(metaclass=ABCMeta ):
296
+ class C(ABC ):
282
297
@property
283
298
@abstractmethod
284
299
def my_abstract_property(self):
@@ -288,7 +303,7 @@ The :mod:`abc` module also provides the following decorators:
288
303
read-write abstract property by appropriately marking one or more of the
289
304
underlying methods as abstract::
290
305
291
- class C(metaclass=ABCMeta ):
306
+ class C(ABC ):
292
307
@property
293
308
def x(self):
294
309
...
0 commit comments