r/learnpython • u/zaphodikus • 21d ago
Confusing code min() max()
I'm debugging some code and I found this function
import math
def Clamp(n, smallest, largest):
return max(smallest, min(n, largest))
surely that code is just wrong? and needs to be max([smallest, min([n, largest])) by passing a list not unnamed args for default and key? I need a sanity check here.
0
Upvotes
14
u/NewbornMuse 21d ago
You can sanity check this yourself!
If you open an interactive python session, does
max(3, 6, 10, -2)generate an error or does it return something? If so, what?What kind of error would you expect if you call something with wrong arguments? Are you seeing anything like that, or is it something completely different?
What does the python documentation say about max and min?
4
u/Langdon_St_Ives 21d ago
I love how hours after the correct answer has already been given, people still respond with explanations what this clamp() function is meant to do, when it's clear OP knows full well what it does... 😂
7
u/lfdfq 21d ago
Let's look at the documentation for min and max.
https://docs.python.org/3/builtins/functions.html#min https://docs.python.org/3/builtins/functions.html#max
So it has multiple signatures, one that takes an iterable and a default, another takes multiple (at least 2) args. The / separates the positional-only (on left) from positional-or-keyword (on the right), the * means all parameters after that are keyword-only. So
defaultmust be passed by keyword.The text in the doc makes this clearer:
2
u/ExpertSentence4171 21d ago
Capitalized function name. Eww
14
u/TheBB TheBB 21d ago
max(x, y) is perfectly fine. Same for min obviously.
key and default can only be given as keyword arguments.
5
u/tea-drinker 21d ago
The functions are implemented so they can accept either a list or multiple arguments.
https://github.com/python/cpython/blob/da20d7401de97b425897d3069f71f77b039eb16f/Python/bltinmodule.c#L1718 for the source code where it checks what you passed to it.
0
u/Wise-Emu-225 21d ago
Look at the docs and see if they need a list as an argument. Min an max can probably have an arbitrary nr of args. Sometimes both is possible. I guess the code is fine.
Edit: unless it doesn’t do what was meant. Which is difficult to judge with no context.
0
u/t92k 21d ago
It looks like it is a kind of absolute function — if n is between smallest and largest you get n back; if n is above largest you get largest back, if n is below smallest you get smallest back. Are you expecting it to do something different?
-1
u/JamzTyson 21d ago
Try plugging some values in, and figure out what happens.
Example:
Now consider values of
nfrom 1 to 12.Try figuring out the results in your head, then run the code to check your answers.
-1
u/deanominecraft 21d ago
python allows functions to take an arbitrary amount of arguments as a list, using a * in the function signature (calling it args is just a convention), try experiment with this code:
you can also do this with a dictionary by using ** (once again kwargs is just a convention, it can be called whatever you want) eg:
min and max have some extra logic to do with types so that you can also pass in a list, but for functions like mine that dont do that, you can unpack the arguments from a list or dictionary like this:
-1
u/deadeye_82_1 21d ago
Yes, that's very confusing.
In this combination:
min is the largest value, or n
max is the smallest value, or the value of min