で抽象メソッドの本体に何を置くべきは、私は、次の抽象クラスFoo
を持って言う:私は、Python
import abc
class Foo(abc.ABC):
@abc.abstractmethod
def bar(self):
raise NotImplementedError
私はbar
方法の身体に何を置くべき?
上記のように、raise NotImplementedError
のコードが多数あります。ただし、bar
を実装していないサブクラスでは、インスタンス化するときにTypeError: Can't instantiate abstract class Foo with abstract methods bar
が発生するため、これは冗長なようです。
は、次のように、空のbar
を残すためにPython的です:
import abc
class Foo(abc.ABC):
@abc.abstractmethod
def bar(self):
...
これはAbstract Base ClassesのためのPythonドキュメントで行われているものですが、それはただのプレースホルダや、実際の例だ場合、私はわからないんだけどコードを書く方法。
bar
を3ドット(...
)だけ残しても問題ない場合は、いつNotImplementedError
を使用しますか?
感謝を。 [MutableSet.add](https://hg.python.org/cpython/file/3.5/Lib/_collections_abc.py#l508)と[MutableSet.discard](https://hg.python.org/cpython/file /3.5/Lib/_collections_abc.py#l513) 'NotImplementedError'を発生させるので、' ... 'の代わりにその規約に従うべきだと思います。 – ostrokach