[Python-ideas] abc.optionalabstractmethod

alex23 wuwei23 at gmail.com
Tue Aug 7 04:18:33 CEST 2012


On Aug 7, 10:35 am, Steven D'Aprano <st... at pearwood.info> wrote:
> But even putting that aside, the interface that it (implicitly?) documents is
> surely *required* interface. If you can neglect to override an abstract method
> with impunity, then it shouldn't have been an abstract method in the first place.

This I completely agree with. I don't understand the point of
declaring an abstract method that you cannot guarantee will be on an
implementation.

Wouldn't it make more sense to define the optional aspect as a
secondary interface?

    import abc

    class IStarter(object):
        __metaclass__ = abc.ABCMeta

        @abc.abstractmethod
        def start(self):
            """start it up"""

    class IStopper(object):
        __metaclass__ = abc.ABCMeta

        @abc.abstractmethod
        def stop(self):
            """shut it down"""

    class StartOnly(object):
        def start(self):
            print "starting!"

    class StartAndStop(object):
        def start(self):
            print "starting!!"
        def stop(self):
            print "stopping!!"

    IStarter.register(StartOnly)

    IStarter.register(StartAndStop)
    IStopper.register(StartAndStop)



More information about the Python-ideas mailing list