Preventing a class from direct instantiation in Python Preventing a class from direct instantiation in Python python python

Preventing a class from direct instantiation in Python


I would override __new__() in the base class and simply fail to instantiate at all if it's the base class.

class BaseClass:    # Py3    def __new__(cls, *args, **kwargs):        if cls is BaseClass:            raise TypeError(f"only children of '{cls.__name__}' may be instantiated")        return object.__new__(cls, *args, **kwargs)

This separates concerns a little better than having it in __init__(), and "fails fast."


Your approach is a typical framework pattern.

Using __init__ to verify that type(self) is not SuperClass is a reasonable way to make sure the SuperClass hasn't been instantiated directly.

The other common approach is to provide stub methods that raise NotImplementedError when called. That is more reliable because it also validates that subclasses have overridden the expected methods.


This is what I might do:

class SuperClass(object):    def __init__(self):        if type(self) == SuperClass:            raise Exception("<SuperClass> must be subclassed.")        # assert(type(self) == SuperClass)class SubClass(SuperClass):    def __init__(self):        SuperClass.__init__(self)subC = SubClassOne()supC = SuperClass() # This line should throw an exception

When run (exception is thrown!):

[ 18:32 jon@hozbox ~/so/python ]$ ./preventing-direct-instantiation.pyTraceback (most recent call last):  File "./preventing-direct-instantiation.py", line 15, in <module>    supC = SuperClass()  File "./preventing-direct-instantiation.py", line 7, in __init__    raise Exception("<SuperClass> must be subclassed.")Exception: <SuperClass> must be subclassed.

Edit (from comments):

[ 20:13 jon@hozbox ~/SO/python ]$ cat preventing-direct-instantiation.py #!/usr/bin/pythonclass SuperClass(object):    def __init__(self):        if type(self) == SuperClass:            raise Exception("<SuperClass> must be subclassed.")class SubClassOne(SuperClass):    def __init__(self):        SuperClass.__init__(self)class SubSubClass(SubClassOne):    def __init__(self):        SubClassOne.__init__(self)class SubClassTwo(SubClassOne, SuperClass):    def __init__(self):        SubClassOne.__init__(self)        SuperClass.__init__(self)subC = SubClassOne()try:    supC = SuperClass()except Exception, e:    print "FAILED: supC = SuperClass() - %s" % eelse:    print "SUCCESS: supC = SuperClass()"try:    subSubC = SubSubClass()except Exception, e:    print "FAILED: subSubC = SubSubClass() - %s" % eelse:    print "SUCCESS: subSubC = SubSubClass()"try:    subC2 = SubClassTwo()except Exception, e:    print "FAILED: subC2 = SubClassTwo() - %s" % eelse:    print "SUCCESS: subC2 = SubClassTwo()"

Prints:

[ 20:12 jon@hozbox ~/SO/python ]$ ./preventing-direct-instantiation.py FAILED: supC = SuperClass() - <SuperClass> must be subclassed.SUCCESS: subSubC = SubSubClass()SUCCESS: subC2 = SubClassTwo()