The better way to do something depends on the context.
In case of sum and other aggregate function with arbitrary number of values the best way would be to do it as Python does, i.e. pass a list:
sum([1,2,3]) # this is how `sum` function works in python
def my_aggregate_function(values: Iterable[float]):
...
Some people have mentioned that "It's a bad practice to use many arguments". No it is not. The bad practice in programming would be following any guidelines mindlessly. Trying to shuffle some args under the carpet to suppress a linter warning is a terrible thing to do. Don't do that.
Sometimes it's indeed useful to create a dataclass or other structure to group multiple arguments. For example:
def create_user(
user_first_name: str,
user_phone: str,
user_email: str,
... # and so on
):
...
You see a lot of parameters bound to a user. here. Why not create a User struct and pass it to the method instead?
# using a typed dict her just as an example
class User(TypedDict):
fist_name: str
phone: str
email: str
def create_user(user: User):
...
By doing so you can extend user properties in the future without rewriting each function which requires a user.
Or suppose you have something like a search function accepting a lot of independent parameters. Filters, pagination, query string, etc:
# 6 args - just above pylint defaults
def find(
query: str,
conditions: list,
offset: int = 0,
limit: int = 10,
sort_key: str = None,
sort_order: str = None
):
...
This function serves a specific task and uses a specific set of parameters. You can create a Query class to hold those but why? So instead of just holding Ctrl on the keyboard a dev would need to go to the function source to see what's in the query params?
Sure, you suppressed a warning and the commit has been accepted and you are happy. But while solving this artificial problem you forgot about real ones. For example, how a dev would know what sort_order
can be passed there? asc
and desc
? Or maybe ASC
and DESC
? What structure should be passed in a list of conditions
? The linter won't warn you about those ones. That's why you need to use your head and not just style guides.