Which Exception for notifying that subclass should implement a method?
In Python 2.6 and better, you can use the abc module to make Base
an "actually" abstract base class:
import abc
class Base:
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def g(self):
pass
def f(self): # &c
this guarantees that Base
cannot be instantiated -- and neither can any subclass which fails to override g
-- while meeting @Aaron's target of allowing subclasses to use super
in their g
implementations. Overall, a much better solution than what we used to have in Python 2.5 and earlier!
Side note: having Base inherit from object would be redundant, because the metaclass needs to be set explicitly anyway.
Make a method that does nothing, but still has a docstring explaining the interface. Getting a NameError
is confusing, and raising NotImplementedError
(or any other exception, for that matter) will break proper usage of super
.