Hacker News new | ask | show | jobs
Ask HN: Large number of function arguments in Python
1 points by gcells 2583 days ago
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!

1 comments

Check out dataclasses[1]. It's in the standard library of Python 3.7 and there's a backport for earlier versions.

https://docs.python.org/3/library/dataclasses.html

Thanks, will look into it!