What does pylint's "Too few public methods" message mean

monsur picture monsur · Dec 25, 2012 · Viewed 62.7k times · Source

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:

Used when class has too few public methods, so be sure it's really worth it.

Answer

Blender picture Blender · Dec 25, 2012

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.