<p dir="ltr">I dislike this API. What's the point of calling clamp(x)? clamp(b, a) is min(a, b) and clamp(a, max_val=b) is just max(a, b). My point is that all  parameters must be mandatory.</p>
<p dir="ltr">Victor</p>
<div class="gmail_quote">Le 31 juil. 2016 6:41 AM, "David Mertz" <<a href="mailto:mertz@gnosis.cx">mertz@gnosis.cx</a>> a écrit :<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><p dir="ltr" style="font-size:14px">Is there some special subtlety or edge case where a hand rolled function will go wrong? I like the SO version spelled like this (a little fleshed out):</p><p dir="ltr" style="font-size:14px">def clamp(val, min_val=None, max_val=None):<br>    min_val = val if min_val is None else min_val<br>    max_val = val if max_val is None else max_val<br>    assert min_val <= max_val<br>    return max(min(val , max_val), min_val)</p><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 30, 2016 at 2:57 PM, Neil Girdhar <span dir="ltr"><<a href="mailto:mistersheik@gmail.com" target="_blank">mistersheik@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">It's common to want to clip (or clamp) a number to a range.  This feature is commonly needed for both floating point numbers and integers:<div><br></div><div><a href="http://stackoverflow.com/questions/9775731/clamping-floating-numbers-in-python" target="_blank">http://stackoverflow.com/questions/9775731/clamping-floating-numbers-in-python</a><br></div><div><a href="http://stackoverflow.com/questions/4092528/how-to-clamp-an-integer-to-some-range-in-python" target="_blank">http://stackoverflow.com/questions/4092528/how-to-clamp-an-integer-to-some-range-in-python</a></div><div><br></div><div>There are a few approaches: <div><br></div><div>* use a couple ternary operators (e.g. <a href="https://github.com/scipy/scipy/pull/5944/files" target="_blank">https://github.com/scipy/scipy/pull/5944/files</a>  line 98, which generated a lot of discussion)</div><div>* use a min/max construction,</div><div>* call sorted on a list of the three numbers and pick out the first, or</div><div>* use numpy.clip. </div><div><br></div><div>Am I right that there is no *obvious* way to do this?  If so, I suggest adding math.clip (or math.clamp) to the standard library that has the meaning:</div><div><br></div><div>def clip(number, lower, upper):</div><div>    return lower if number < lower else upper if number > upper else number</div><div><br></div><div>This would work for non-numeric types so long as the non-numeric types support comparison.  It might also be worth adding</div><div><br></div><div>assert lower < upper</div><div><br></div><div>to catch some bugs.</div><div><br></div><div>Best,</div><div><br></div><div>Neil</div></div></div><br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div data-smartmail="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div>