Interfaces (a la PEP 245 and Zope)
heikowu at ceosg.de
Fri Aug 1 12:53:16 CEST 2003
Simplest way I use to implement "interfaces":
raise NotImplementedError("Implement this for IEditor")
# Other mandatory functions.
# Implementation of getSelection overrides "virtual" base.
Checking whether a class "implements" an interface can be done with
issubclass(Editor,IEditor) or isinstance(editinst,IEditor).
This doesn't do rigid checking of an interface, but seems to suit most
of my needs. If you devise a test-suite for your program, you'll catch
the NotImplementedError inevitably when you run the code.
I don't really know what __implements__ does aditionally in Zope, as
I've never programmed for/with it, but I'd consider the above paradigm,
it's much simpler and "just works", without extra modules.
More information about the Python-list