Skip to content

Commit 7493c86

Browse files
author
Scott Sanderson
committed
DOC: Docs for interface subclassing.
1 parent 87faa0b commit 7493c86

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/usage.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,42 @@ of ``get_all``:
172172

173173
Consider changing ReadOnlyMapping.get_all or making these attributes part of ReadOnlyMapping.
174174
class ReadOnlyMapping(interface.Interface):
175+
176+
Interface Subclassing
177+
~~~~~~~~~~~~~~~~~~~~~
178+
179+
Interfaces can inherit requirements from other interfaces via subclassing. For
180+
example, if we want to create interfaces for read-write and read-only mappings,
181+
we could do so as follows:
182+
183+
.. code-block:: python
184+
185+
class ReadOnlyMapping(interface.Interface):
186+
def get(self, key):
187+
pass
188+
189+
def keys(self):
190+
pass
191+
192+
193+
class ReadWriteMapping(ReadOnlyMapping):
194+
195+
def set(self, key, value):
196+
pass
197+
198+
def delete(self, key):
199+
pass
200+
201+
202+
An interface that subclasses from another interface inherits all the function
203+
signature requirements from its parent interface. In the example above, a class
204+
implementing ``ReadWriteMapping`` would have to implement ``get``, ``keys``,
205+
``set``, and ``delete``.
206+
207+
.. warning::
208+
209+
Subclassing from an interface is not the same as implementing an
210+
interface. Subclassing from an interface **creates a new interface** that
211+
adds additional methods to the parent interface. Implementing an interface
212+
creates a new class whose method signatures must be compatible with the
213+
interface being implemented.

0 commit comments

Comments
 (0)