Issue 36788: Add clamp() function to builtins (original) (raw)

It would be nice to have a clamp() builtin in addition to min() and max() so one can type e.g. "clamp(value, 2, 5)" instead of having to type "min(max(value, 5), 2)".

I doubt this is important enough to go into builtins, the only practical use-case I know of for this function is with numbers, so this could go in the math module.

But putting that aside, there are some other problems:

min(max('a string', 'd'), 'm') => returns 'd' not ['d', 'd', 'm', 'm', 'm', 'i', 'm', 'g']

[clamp(x, a, b) for x in values]

You should read over this thread here:

https://mail.python.org/pipermail/python-ideas/2016-July/041262.html

Maybe port this discussion over python-ideas and after the resolution open it again?

Sounds good to me. Adding a new builtin function is a big deal that would likely need a PEP. I'm not keen on adding something like this to the math module, either, when it's so simple to construct from max and min.

Closing as rejected here; it can be re-opened if there's a strong consensus for adding it on the ideas list.