9

I understand from this answer why the warning exists. However, why would the default value of it be 2?

It seems to me that classes with a single public method aside from __init__ are perfectly normal! Is there any caveat to just setting

min-public-methods=1

in the pylintrc file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bluehallu
  • 10,205
  • 9
  • 44
  • 61

2 Answers2

10

The number 2 is completely arbitrary. If min-public-methods=1 is a more fitting policy for your project and better matches your code esthetic opinions, then by all means go for it. As was once said, "Pylint doesn't know what's best".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user7610
  • 25,267
  • 15
  • 124
  • 150
0

For another perspective, Jack Diederich gave a talk at PyCon 2012 called "Stop Writing Classes".

One of his examples is the class with a single method, which he suggests should be just a function. If the idea is to set up an object containing a load of data and a single method that can be called later (perhaps many times) to act on that data, then you can still do that with a regular function by making an inner function the return value.

Something like:

def complicated(a, b, c, d, e):
    def inner(k):
       return (a*k, b*k, c*k, d*k, e*k)
    return inner
foo = complicated(1, 2, 3, 4, 5)
result = foo(100)

This does seem much simpler to me than:

class Complicated:
    def __init__(self, a, b, c, d, e):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.e = e

    def calc(self, k)
        return (self.a*k, self.b*k, self.c*k, self.d*k, self.e*k)

foo = Complicated(1, 2, 3, 4, 5)
result = Complicated.calc(100)

The main limitation of the function based approach is that you cannot read back the values of a, b, c, d, and e in the example.

Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
  • IDK if this is still true today now that we have dataclasses. – Joshua Jones Oct 03 '22 at 21:48
  • @JoshuaJones A dataclass would only remove the need for the `__init__` method. And in most 'real life' cases part of the reason for this kind of thing (not shown in my example) is that you pre-calculate a load of stuff when you set the object up. So using a dataclass you'd probably end up having to define a `__post_init__` method and you are back to square one. The beauty of the inner function approach is its simplicity and economy. – Ian Goldby Oct 04 '22 at 08:02
  • Saying the first options is much simpler is very subjective. I'd argue the second option allows me to read the function in blocks (initialization vs. execution). Of course, for such toy examples, it's hard to see it, but if your `__init__` sets up anything serious, it immediately becomes clear. And if execution requires several steps and depends on other objects, it would be madness to pack it all into a single function, but a class with a single public method and multiple private methods would be easily more readable – Neowizard Jul 02 '23 at 14:07
  • @Neowizard There's nothing to stop you splitting the initialisation into multiple inner functions. We've all been conditioned into thinking classes are the [golden hammer](https://en.wikipedia.org/wiki/Law_of_the_instrument). That might be true in some languages, but not Python. – Ian Goldby Jul 03 '23 at 07:15
  • @IanGoldby I think 3 level of function nesting before we consider the nesting for logic/control flow isn't the right solution. At this point, it would feel to me (as a reader/reviewer) that the coder was actively avoiding making a class, and I'd start to try and figure out whether there was an actual reason for this, distracting me further from the actual code. – Neowizard Jul 05 '23 at 12:08
  • For people who come from strongly typed languages and are thus not used to such magic that's pretty much only possible in python (and maybe JS), the second solution is way more readable... – AlexGeorg Aug 31 '23 at 09:48
  • @AlexGeorg Perhaps, but if you take that approach seriously then you would avoid any Python feature that doesn't exist in other languages because devs coming from other languages would not find it readable. So then why use Python at all? Python code is supposed to look like Python, not C#. – Ian Goldby Aug 31 '23 at 11:20