Skip to content

Chain Compares

Sourcery refactoring id: chain-compares

Description

Combine two compares on same value into a chained compare

Before

if 1 < b and b < 3:
    print("b is between 1 and 3")

After

if 1 < b < 3:
    print("b is between 1 and 3")

Explanation

Two compare operations on the same value can be chained together. This is described in detail in the Python comparison docs.

Chaining compares is how they are written in mathematics and makes them easier to read.