Pythonic Abstract Base Class / Interface

Bruno Desthuilliers bdesth.nospam at removeme.free.fr
Tue Oct 28 14:51:37 EST 2003


Tom Evans wrote:
> 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?

c), unless you have a compelling technical reason -> then a) with 
NotImplementedErrors or like... I cannot see what's the b) option would 
buy you anyway.

Work with the language, not against it.

Bruno





More information about the Python-list mailing list