Or If Expression Identity¶
Sourcery refactoring id: or-if-exp-identity
¶
Description:¶
Replaces if
expressions that compare to the target with or
Before:¶
currency = input_currency if input_currency else DEFAULT_CURRENCY
After:¶
currency = input_currency or DEFAULT_CURRENCY
Explanation:¶
Here we find ourselves setting a value if it evaluates to True
, and otherwise
using a default.
The 'After' case is a bit easier to read and avoids the duplication of
input_currency
.
It works because the left-hand side is evaluated first. If it evaluates to
True
then currency will be set to this and the right-hand side will not be
evaluated. If it evaluates to False
the right-hand side will be evaluated and
currency
will be set to DEFAULT_CURRENCY
.