[Python-ideas] A posteriori implementation of an abstract method

Antony Lee anntzer.lee at gmail.com
Fri May 24 04:10:50 CEST 2013


Currently, the implementation of abstract methods is not possible outside
of the class statement:

from abc import ABCMeta, abstractmethod
class ABC(metaclass=ABCMeta):
    @abstractmethod
    def method(self): pass
class C(ABC): pass
C.method = lambda self: "actual implementation"
C()

results in a TypeError (complaining about abstract methods), as
__abstractmethods__ is just computed once, at class definition.

Of course this example is a bit contrived, but perhaps a more legitimate
use case would involve a class decorator or another way to define the
implementation of the abstract methods out of the class, such as

@implementation(C, ABC)
def method():
    return "actual implementation"

I believe this behavior can be "fixed" (well, I don't know yet if this
should actually be considered an error, and haven't actually tried to "fix"
it) by defining ABCMeta.__setattr__ properly (to update __abstractmethods__
when necessary).  What do you think?

Best,
Antony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130523/22629bbc/attachment.html>


More information about the Python-ideas mailing list