Using the "everything else" notations of * and **
Python offers even more flexibility in how we can define positional and keyword parameters for a function. The examples we've seen are all limited to a fixed and finite collection of argument values. Python allows us to write functions that have an essentially unlimited number of positional as well as keyword argument values.
Python will create a tuple
of all unmatched positional parameters. It will also create a dictionary of all unmatched keyword parameters. This allows us to write functions that can be used like this:
>>> prod2(1, 2, 3, 4) 24
This function accepts an arbitrary number of positional arguments. Compare this with the prod()
function shown previously. Our previous example required a single sequence object, and we had to use that function as follows:
>>> prod([1, 2, 3, 4]) 24
The prod2()
function will create a product of all argument values. Since the prod2()
function can work with an unlimited collection of positional...