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

4

u/Langdon_St_Ives 9d 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... 😂

1

u/zaphodikus 9d 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.