If and Inline if, what are the advantages and disadvantages?

后端 未结 3 1506
梦如初夏
梦如初夏 2021-01-01 03:36

I\'m a little curious about the difference between if and inline if, in Python. Which one is better?

Is there any reason to use inline if, other than the f

3条回答
  •  自闭症患者
    2021-01-01 03:55

    Inline if is an expression, so you can not put assignments inside.

    Correct syntax would be:

    a = a*2 if b == 2 else a/w
    

    As for the usefulness, it's a question of style, and perhaps it would be a good question for Programmers StackExchange.

提交回复
热议问题