Shorter, more pythonic way of writing an if statement

后端 未结 6 1693
情书的邮戳
情书的邮戳 2021-01-30 08:38

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

6条回答
  •  礼貌的吻别
    2021-01-30 09:35

    You can use,

    a = b if c else d 
    

    but if you are using a python version prior to 2.5,

    bc = c.page == "blog" and "on" or "off"
    

    can do the trick also.

提交回复
热议问题