I'm running Pylint on some code, and receiving the error "Too few public methods (0/2)". What does this message mean?
The Pylint documentation is not helpful:
Used when a class has too few public methods, so be sure it's really worth it.
I'm running Pylint on some code, and receiving the error "Too few public methods (0/2)". What does this message mean?
The Pylint documentation is not helpful:
Used when a class has too few public methods, so be sure it's really worth it.
エラーは基本的に、クラスを辞書として扱っているため、クラスはデータを格納するだけのものではないことを示しています。クラスには、保持するデータを操作するためのメソッドが少なくともいくつか必要です。
クラスが次のようになっている場合:
class MyClass(object):
def __init__(self, foo, bar):
self.foo = foo
self.bar = bar
namedtuple
代わりに辞書またはを使用することを検討してください。クラスが最良の選択のように思われる場合は、それを使用してください。Pylint は常に何が最善かを知っているわけではありません。
namedtuple
は不変であり、インスタンス化で割り当てられた値は後で変更できないことに注意してください。
クラスを拡張している場合は、この警告を体系的に無効にして先に進むことをお勧めします。たとえば、Celery タスクの場合は次のようになります。
class MyTask(celery.Task): # pylint: disable=too-few-public-methods
"""base for My Celery tasks with common behaviors; extends celery.Task
...
1 つの関数のみを拡張する場合でも、この手法を機能させるにはクラスが必要であり、サードパーティのクラスをハッキングするよりも拡張する方が確実に優れています。