refactoring – Python: avoiding Pylint warnings about too many arguments

refactoring – Python: avoiding Pylint warnings about too many arguments

First, one of Perliss epigrams:

If you have a procedure with 10
parameters, you probably missed some.

Some of the 10 arguments are presumably related. Group them into an object, and pass that instead.

Making an example up, because theres not enough information in the question to answer directly:

class PersonInfo(object):
  def __init__(self, name, age, iq): = name
    self.age = age = iq

Then your 10 argument function:

def f(x1, x2, name, x3, iq, x4, age, x5, x6, x7):


def f(personinfo, x1, x2, x3, x4, x5, x6, x7):

and the caller changes to:

personinfo = PersonInfo(name, age, iq)
result = f(personinfo, x1, x2, x3, x4, x5, x6, x7)

Do you want a better way to pass the arguments or just a way to stop Pylint from giving you a hard time? If the latter, you can stop the nagging by putting Pylint-controlling comments in your code along the lines of:

#pylint: disable=R0913

or, better:

#pylint: disable=too-many-arguments

remembering to turn them back on as soon as practicable.

In my opinion, theres nothing inherently wrong with passing a lot of arguments and solutions advocating wrapping them all up in some container argument dont really solve any problems, other than stopping Pylint from nagging you :-).

If you need to pass twenty arguments, then pass them. It may be that this is required because your function is doing too much
and refactoring could assist there, and thats something you should look at. But its not a decision we can really make unless we see what the real code is.

refactoring – Python: avoiding Pylint warnings about too many arguments

You can easily change the maximum allowed number of arguments in Pylint. Just open your pylintrc file (generate it if you dont already have one) and change:

max-args = 5


max-args = 6 # Or any value that suits you

From Pylints manual

Specifying all the options suitable
for your setup and coding standards
can be tedious, so it is possible to
use a rc file to specify the default
values. Pylint looks for /etc/pylintrc
and ~/.pylintrc. The –generate-rcfile
option will generate a commented
configuration file according to the
current configuration on standard
output and exit. You can put other
options before this one to use them in
the configuration, or start with the
default values and hand tune the

Leave a Reply

Your email address will not be published. Required fields are marked *