![](https://secure.gravatar.com/avatar/81582f7d425bddd76e3f06b44aa62240.jpg?s=120&d=mm&r=g)
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: