Disallowing instantiation of super class

Irv Kalb Irv at furrypants.com
Fri Feb 24 18:31:56 EST 2017


> On Feb 24, 2017, at 1:24 AM, Steve D'Aprano <steve+python at pearwood.info> wrote:
> 
> On Fri, 24 Feb 2017 11:19 am, Irv Kalb wrote:
> 
>> Hi,
>> 
>> I have built a set of three classes:
>> 
>> -  A super class, let's call it: Base
> [...]
>> I would like to add is some "insurance" that I (or someone else who uses
>> my code) never instantiates my Base class,  It is not intended to be
>> instantiated because some of the needed instance variables are only
>> created in the __init__ method of ClassA and ClassB.
> 
> class Base:
>   def __init__(self):
>       if type(self) is Base:
>           raise TypeError("cannot instantiate Base class")
> 
> 
> Does that help?
> 
> 

Double D'oh!!

That's exactly what I was looking for.  (Why didn't I think of that?)

Thanks very much!

Irv




More information about the Python-list mailing list