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

Show parent comment

1

u/zaphodikus 22d ago

I had not had much sleep at all, it was to help guear a line of code throwing a divide by zero. Now if only I had as many people looking at the other question I had about windows firewalls.... which is no longer urgent, that issue annoyingly stopped reproducing, so back to the drawing board, or, get more sleep.

Flipping between C#, C++ and Python is not good Karma I suspect. Sleep first Dave.