[Python-Dev] One-line abstractmethod function?

Allen Li cyberdupo56 at gmail.com
Thu Dec 5 19:20:22 CET 2013


Hello Python devs,

As a regular Python user, I find the abc module useful for making
Python's duck typing more explicit.  In particular, I ofen use it
like a Java interface or C header, to provide methods to implement for a
given "duck type".

90% of the time, it ends up looking something like this:

class Foo(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def f1(self):
        raise NotImplementedError

    @staticmethod
    @abc.abstractmethod
    def f2(arg1):
        raise NotImplementedError

    ...

What if there was a function, say make_abstract_method (better name
pending), so that the above could be written like:

class Foo(metaclass=abc.ABCMeta):

    f1 = abc.make_abstract_method('f1', ['self'])
    f2 = staticmethod(abc.make_abstract_method('f2', ['arg1']))
    ...

I think that it would make ABC definitions a lot more compact for many
use cases, but I welcome any criticisms against this idea.

Allen Li


More information about the Python-Dev mailing list