idioms for abstract base classes

Robin Becker robin at jessikat.fsnet.co.uk
Fri Apr 13 07:04:24 EDT 2001


what's the best way to spell an abstract base class so that
0) base is B and A inherits from B

1) doing B() causes an exception

2) most of the initialisation code is common in B


I thought of

class B:
   def __init__(self,*args,**kw):
       raise NotImplementedError, '%s is an abstract class' % self.__class__.__name__

   def __INIT__(self,*args,**kw):
       pass

class A(B):
   def __init__(self,*args,**kw):
       B.__INIT__(self,*args,**kw)
       # more

but this causes problems if class A doesn't need/implement an __init__; so is there a neater way
to do this that knows when B is being instantiated directly?
-- 
Robin Becker



More information about the Python-list mailing list