Is short-circuiting in assignment statements considered good style?

前端 未结 2 1005
清酒与你
清酒与你 2020-12-20 22:36

If I understand correctly

myvar = a and b or c

gives the same result as

if a:
  if b:
    myvar = b
  else:
    myvar = c
e         


        
2条回答
  •  无人及你
    2020-12-20 22:59

    This is really an opinion question, but for the most part, the answer is no. It goes against multiple style guides, probably because people tend to think it means "if a is true, use the value of b, otherwise use the value of c" rather than the real meaning, which is what you posted.

    You probably want the new-ish conditional expression syntax instead:

    myvar = b if a else c
    

提交回复
热议问题