On Thu, May 28, 2020 at 11:40:52AM +0300, Serhiy Storchaka wrote:
Why do you think it will be faster? `a == b == c` is the same as `a == b and b == c`, but a tiny bit slower.
That surprises me. I thought that the big advantage of chained comparisons is that results are only evaluated once.
a == expensive_expression == c
should be faster than calculating the expensive expression twice:
a == expensive_expression and expensive_expression == c
and even if the expression is just a name lookup, isn't one name lookup cheaper than two?
You always can write
self.assertTrue(a == b == c)
But the advantage of two separate assertions is that you know what comparison fails if it fails and get more informative report.
I agree that this is a very good argument for writing separate assertions.