Skip to content

Files

Latest commit

 

History

History
23 lines (14 loc) · 639 Bytes

consider-using-ternary.md

File metadata and controls

23 lines (14 loc) · 639 Bytes

Pattern: Use of old ternary syntax

Issue: -

Description

Before Python 2.5, a common idiom for ternary was to use logical operators:

[expression] and [on_true] or [on_false]

However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the ... if ... else ... form:

x, y = 50, 25
small = x if x < y else y

Further Reading