I have this
bc = \'off\'
if c.page == \'blog\':
bc = \'on\'
print(bc)
Is there a more pythonic (and/or shorter) way of writing this in Pyth
This is:
X if Z else Y
syntax)questionably readable. With those caveats in mind, here it goes:
bc = ("off","on")[c.page=="blog"]
EDIT: As per request, the generalized form is:
result = (on_false, on_true)[condition]
Explanation: condition
can be anything that evaluates to a Boolean. It is then treated as an integer since it is used to index the tuple: False == 0
, True == 1
, which then selects the right item from the tuple.