Pythonic Abstract Base Class / Interface

Tom Evans tom_evans_a at uk.co.yahoo.reverse
Tue Oct 28 07:16:58 EST 2003


My basic question:
If I have a specific interface which I know is going to be implemented 
by a number of classes, but there is no implementation commonality 
between them, what is the preferred form for this in Python?

In a staticly typed language like c++ or java, I'd describe the interface 
first, then create the classes eithered derived from that ABC or 
implementing that interface (same thing really).  This was the first 
thing I thought to do in python, but then rapidly realised I didn't 
actually need to write anything in the base class - as long as code using 
one of the implemented classes just tries to do things with it it 
would just work.  In fact, I didn't even need to make a base class at 
all if the calling code never checks for type (as seems normal).

So, as I see it I could:
a) create a base class with stubs for all the interface and derive from that.
b) create an empty base class and derive from that.
c) just make the concrete classes directly with no inheritance commonality.

Is there a clear pythonic way, or does it depend a bit more on the 
design of the specific program?

Tom




More information about the Python-list mailing list