...

/

Optimizing the Number of Arguments in Functions

Optimizing the Number of Arguments in Functions

Learn about optimizing the number of arguments in Python functions.

Having functions or methods that take too many arguments is a sign of bad design (a code smell). Let’s explore ways of dealing with this issue.

The first alternative is a more general principle of software design—reification (creating a new object for all of those arguments that we are passing, which is probably the abstraction we are missing). Compacting multiple arguments into a new object is not a solution specific to Python, but rather something that we can apply in any programming language.

Press + to interact

Another option is to use the Python-specific features we saw in the previous lesson, making use of variable positional and keyword arguments to create functions that have a dynamic signature. While this might be a Pythonic way of proceeding, we have to be careful not to abuse the feature, because we might be creating something that is so dynamic that it is hard to maintain. In this case, we should take a look at the body of the function. Regardless of the signature and whether the parameters seem to be correct, if the function is doing too many different things responding to the values of the parameters, then that is a sign ...