[Python-ideas] abc.optionalabstractmethod

Simon Sapin simon.sapin at kozea.fr
Thu Aug 2 17:37:50 CEST 2012

Le 02/08/2012 17:26, Eric Snow a écrit :
> Sometimes you want to specific an optional method in an abstract base
> class.  Currently we don't have a consistent way of doing so, instead
> having to mix in the old way of defining "abstract" methods:
> class MyABC(metaclass=ABCMeta):
>      ...
>      def do_something_optional(self):
>          """An optional method for doing something."""
>          raise NotImplementedError


What’s wrong with this?

I think that the only thing making methods with @abstractmethod special 
is that they prevent instantiation if they are not overridden. If we 
remove that, the only remaining difference is terms of documentation. 
Maybe a new Sphinx directive could help?

What would an optional abstract method do when it is called? Raise 

I’m not against this idea but I just don’t see how 
@optionalabstractmethod is better than raise NotImplementedError

Simon Sapin

More information about the Python-ideas mailing list