[Tutor] interfaces and abstract classes in python

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 6 16:19:48 CET 2005


> Interfaces and abstract classes - I know they don't exist per se in 
> Python. 

First you need to define what you mean by the terms.
Every class has an interface - it is the set of messages to which 
it responds.

An Abstract class is one which is not intended to be instantiated.

class AbstractClassError(Exception): pass

class Abstract:
    def __init__(self): raise AbstractClassError

> But what are the closest analogues? I've found a few examples, 

Assuming you mean Interface in the Microsoft/Java specific sense 
of the term rather than the simple OOP sense, then an Interface 
class is simply an abstract  class with empty methods.

class InterfaceError(Exception): pass

class Interface(Abstract):
    def myMethod(self): pass
    def myOther(self): raise InterfaceErrror

Does that do what you want?

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list