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.”

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)