What should I put in the body of an abstract method in Python

前端 未结 1 1942
迷失自我
迷失自我 2020-12-06 18:45

Say I have the following abstract class Foo:

import abc

class Foo(abc.ABC):

    @abc.abstractmethod
    def bar(self):
        raise NotImplem         


        
相关标签:
1条回答
  • 2020-12-06 18:52

    The documentation does aim to give you an example. You don't have to follow it.

    You could provide a default; subclasses are still free to use super() to call your implementation. This is what most of the collections.abc classes do; see the source code.

    Size for example, returns 0 for __len__:

    class Sized(metaclass=ABCMeta):
        # ...
        @abstractmethod
        def __len__(self):
            return 0
    
    0 讨论(0)
提交回复
热议问题