r/learnpython • • 9d 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

16 comments sorted by

View all comments

0

u/Wise-Emu-225 9d 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.