> On 9 Jul 2020, at 21:04, Ethan Furman <et...@stoneleaf.us> wrote:
>
> On 07/03/2020 05:03 PM, Steven D'Aprano wrote:
>
>> def clamp(value, lower, upper):
>> """Clamp value to the closed interval lower...upper.
>> The limits lower and upper can be set to None to
>> mean -∞ and +∞ respectively.
>> """
>> if not (lower is None or upper is None):
>> if lower > upper:
>> raise ValueError('lower must be <= to upper')
>> if lower == upper is not None:
>> return lower
>> if lower is not None and value < lower:
>> value = lower
>> elif upper is not None and value > upper:
>> value = upper
>> return value
>
> I'm having a hard time understanding this line:
>
> if lower == upper is not None:
>
> As near as I can tell, `upper is not None` will be either True or False,
> meaning the condition will only ever be True if `lower` is also either True
> or False, and since I would not expect `lower` to ever be True or False, I
> expect this condition to always fail. Am I missing something?
This uses comparison chaining and is equivalent to “lower == upper and upper is
not None”. I don’t like this particular style, I had to read this a couple of
times to get it.
Ronald
—
Twitter / micro.blog: @ronaldoussoren
Blog: https://blog.ronaldoussoren.net/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/QCWO2RMSQPP6YPTAOBRX7AMB3VJ4ZDJT/
Code of Conduct: http://python.org/psf/codeofconduct/