Disallowing instantiation of super class

Steve D'Aprano steve+python at pearwood.info
Fri Feb 24 04:24:27 EST 2017


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?





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list