from Hacker News

Ask HN: Large number of function arguments in Python

by gcells on 5/22/19, 10:56 AM with 2 comments

Hi all,

So I am working on a python application that can take its input as cli args and as a JSON string. The number and type of arguments is the same for both.

I have a function, lets call it _main that is coordinating with other modules that needs all these parameters as input. Currently I am passing ~15 arguments to _main. It looks nasty, and I believe there are better ways to accomplish this.

One way could be to create a request class having @property defined for all these parameters. I can create an object by using parameters from CLI or JSON as required and pass this object to _main.

The downside is that _main is called only at one place. Is it worth the effort to create a class that will be used only once? Or I am better off creating a dict and passing as kwargs this function.

Most but not all of these parameters are needed by other modules that are called by _main. So, the request object created cannot be passed as is to other modules.

What are the best practices around this - to pass large number of arguments to functions?

thanks for taking the time to read this!