On 9 Jul 2020, at 21:04, Ethan Furman <ethan@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/