Assertion & documentation in a class for methods that are expected in derived classes
本问题已经有最佳答案,请猛点这里访问。
假设我生成一个名为bird的类,我只想将它用作父类,派生类应该有一个方法
1 2 3 4 | class Bird: def fly(self): self.flap_wings() |
预期的派生类可能如下所示:
1 2 3 4 | class Eagle(Bird): def flap_wings(self): print('flapping wings') |
对于
现在,我正在使用
1 2 3 4 5 6 7 8 9 10 | class Bird: def fly(self): self.flap_wings() def __init_subclass__(cls, **kwargs): assert hasattr(cls, 'flap_wings'), ( "Derived classes have to have a flap_wings method which should" "print 'flapping wings'." ) |
但是,断言表达式只在创建bird类之后出现,并且不是可以通过
我知道这是一个开放式的问题,但还有什么更好的方法呢?首先在
对于抽象方法,可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from abc import ABCMeta, abstractmethod import six class Bird(six.with_metaclass(ABCMeta)): def fly(self): """Take flight. Notes ----- This depends on the abstract method `flap_wings`. If you've not implemented this at the subclass level, your subclass cannot be properly instantiated. """ self.flap_wings() @abstractmethod def flap_wings(self): """Subclasses must implement this""" |
这就形成了一种契约。任何不实现
1 2 3 4 5 6 7 | class Flamingo(Bird): pass >>> Flamingo() Traceback (most recent call last): File"<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class Flamingo with abstract methods flap_wings |
而实现抽象方法的子类将很好地工作:
1 2 3 4 5 6 | class BlueJay(Bird): def flap_wings(self): print("Flappity flap") >>> BlueJay().fly() Flappity flap |
就记录子类而言,由于所有子类都继承了