On 09.07.20 21:04, Ethan Furman 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?
It's operator chaining and shorthand notation for
(https://docs.python.org/3/reference/expressions.html#comparisons)
if (lower == upper) and upper is not None:
_______________________________________________
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/UHFQDYAZL6INFQZRMU4NAABILZMKMW6N/
Code of Conduct: http://python.org/psf/codeofconduct/