Or-If-Exp-Identity¶
Sourcery rule id: or-if-exp-identity
¶
Description¶
Replace if-expression 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
.