Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Large number of function arguments in Python
1 point by gcells on May 22, 2019 | hide | past | favorite | 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!




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!




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: