See the abc module. Basically, you define __metaclass__ = abc.ABCMeta
on the class, then decorate each abstract method with @abc.abstractmethod
. Classes derived from this class cannot then be instantiated unless all abstract methods have been overridden.
If your class is already using a metaclass, derive it from ABCMeta
rather than type
and you can continue to use your own metaclass.
A cheap alternative (and the best practice before the abc
module was introduced) would be to have all your abstract methods just raise an exception (NotImplementedError
is a good one) so that classes derived from it would have to override that method to be useful.
However, the abc
solution is better because it keeps such classes from being instantiated at all (i.e., it "fails faster"), and also because you can provide a default or base implementation of each method that can be reached using the super()
function in derived classes.