[Python-ideas] abc.optionalabstractmethod

Barry Warsaw barry at python.org
Thu Aug 2 17:31:04 CEST 2012


On Aug 02, 2012, at 09:26 AM, Eric Snow wrote:

>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
>
>This came up in issue 15502[1], where we are splitting
>importlib.abc.Finder into MetaPathFinder and PathEntryFinder.  These
>have a method, invalidate_caches(), which is optional.  It would be
>nice to have a new decorator akin to abstractmethod that would allow
>defining an optional interface in a consistent way.  Something like
>optionalabstractmethod (or some better name).  Then the above example
>would be like this:
>
>class MyABC(metaclass=ABCMeta):
>    ...
>
>    @optionalabstractmethod
>    def do_something_optional(self):
>        """An optional method for doing something."""
>
>Thoughts?

+1

-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120802/6f784679/attachment.pgp>


More information about the Python-ideas mailing list