Skip to content

and-or-ternary (PLR1706)#

Derived from the Pylint linter.

Fix is sometimes available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does#

Checks for uses of the known pre-Python 2.5 ternary syntax.

Why is this bad?#

Prior to the introduction of the if-expression (ternary) operator in Python 2.5, the only way to express a conditional expression was to use the and and or operators.

The if-expression construct is clearer and more explicit, and should be preferred over the use of and and or for ternary expressions.

Example#

x, y = 1, 2
maximum = x >= y and x or y

Use instead:

x, y = 1, 2
maximum = x if x >= y else y