4

This is what the python docs(https://docs.python.org/3/library/functions.html#min) says:

min(iterable, *[, key, default])

But the other fuctions are like this:

iter(object[, sentinel])
pow(base, exp[, mod])
getattr(object, name[, default])

Only max/min has * in its definition.
But for what?
What's the difference between the upper one and the below one?

min(iterable[, key, default])
lwamuhaji
  • 41
  • 2

1 Answers1

2

Difference and meaning

TL;DR: * enforces keywords, key=value, arguments passing.

min(iterable, *[, key, default])

You can read above as min takes iterable as required and first argument, and key and default as optional arguments, meaning you don’t have to pass them, that needs to be passed as key=value pairs. The * forces you to do that. So you cannot pass key or default without clearly stating that it is key or default or both you wish to pass. E.g. min([1,2,3]), min({'a','b'}, key=somekey)

min(iterable, key, default)

Here we mean min takes iterable, key, and default as required arguments. The position of the argument matters as the first is iterable, second key and third default unless you pass them as key=words arguments e.g min(default=somedefault, iterable=(1,2,3), key=somekey),min([1, 3], default=x, key=y). But all three arguments must be passed. As you can see the second can be messy while the first is cleaner.

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57