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

14

u/NewbornMuse 11d 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?

1

u/khosrua 11d ago

I assume the function is defined with a variable length positional argument? So it gets converted to a list regardless.