[Python-Dev] One-line abstractmethod function?

Brett Cannon brett at python.org
Thu Dec 5 21:06:00 CET 2013

On Thu, Dec 5, 2013 at 2:22 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 12/05/2013 10:56 AM, Alexander Belopolsky wrote:
>> On Thu, Dec 5, 2013 at 1:24 PM, Guido van Rossum wrote:
>>> How would you get the docstrings in? [...]
>> One way to reduce the amount of boilerplate code is to make abstractmethod
>> to supply raise NotImplementedError body when none is given.  Then you can
>>  write
>> class Foo:
>>      @abc.abstractmethod
>>      def do_bar(self):
>>           """perform bar"""
>> The docstring will be required when skipping the body which is probably a
>> good thing.
> How will abstractmethod know its function has no body?

Technically you could inspect the code object of the method. to figure out
if the body is empty.

But I would be very leery of this being a default behaviour. Raising
NotImplementedError is not necessarily what the default result should be
for a method. If you are building a class that supports multiple
inheritance you will be calling super().do_bar() almost blindly which, if
you are not careful, will raise NotImplementedError and that might not be
appropriate. Maybe returning None is more reasonable, maybe raising
TypeError. But support a default blindly like this can promote bad

You can see all of changes I had to make for importlib.abc in Python 3.4 to
have API-matching default exceptions and return values instead of blindly
raising NotImplementedError as a lesson learned:
Hunt down Thomas Wouters at PyCon if you want to hear the same arguments he
gave me as to why blindly raise NotImplementedError is bad.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20131205/65880438/attachment-0001.html>

More information about the Python-Dev mailing list