idioms for abstract base classes

Robin Becker robin at jessikat.fsnet.co.uk
Fri Apr 13 10:44:30 EDT 2001


In article <9b6qtv02h2i at news2.newsguy.com>, Alex Martelli
<aleaxit at yahoo.com> writes
>"Robin Becker" <robin at jessikat.fsnet.co.uk> wrote in message
>news:mZW4xJA40t16EwtA at jessikat.fsnet.co.uk...
>> 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
>    [snip]
>> is there a neater way to do this that knows when B is being instantiated
>directly?
>
>Pretty easy:
>
>class B:
>    def __init__(self):
>        if self.__class__ == B:
>            raise AbstractClassException, B
>        # proceed with initialization tasks
>
>self.__class__ is set to the actual (most-derived) class
>that is being instantiated, whether B's __init__ is being
>executed explicitly or through inheritance.
>
>
>Alex
....
yes that's the sort of thing
-- 
Robin Becker



More information about the Python-list mailing list