What does pylint's “Too few public methods” message mean

后端 未结 4 2034
执念已碎
执念已碎 2020-12-24 04:29

I\'m running pylint on some code, and receiving the error \"Too few public methods (0/2)\". What does this message mean? The pylint docs are not helpful:

4条回答
  •  無奈伤痛
    2020-12-24 04:54

    The error basically says that classes aren't meant to just store data, as you're basically treating the class as a dictionary. Classes should have at least a few methods to operate on the data that they hold.

    If your class looks like this:

    class MyClass(object):
        def __init__(self, foo, bar):
            self.foo = foo
            self.bar = bar
    

    Consider using a dictionary or a namedtuple instead. Although if a class seems like the best choice, use it. pylint doesn't always know what's best.

    Do note that namedtuple is immutable and the values assigned on instantiation cannot be modified later.

提交回复
热议问题