Interfaces (a la PEP 245 and Zope)

Heiko Wundram heikowu at ceosg.de
Fri Aug 1 12:53:16 CEST 2003


Hey Terry!

Simplest way I use to implement "interfaces":

class IEditor(object):
	def getSelection(self,start,end):
		raise NotImplementedError("Implement this for IEditor")

	# Other mandatory functions.
	# ...

class Editor(IEditor):
	# 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.

HTH!

Heiko W.






More information about the Python-list mailing list