Python inline elif possible?

前端 未结 7 2249
耶瑟儿~
耶瑟儿~ 2021-02-07 11:16
\'Hello \' + (\'there\' if name is None else name)

Is the equivalent of

msg = \'Hello \'
if name is None:
    msg += \'there\'
else:
          


        
7条回答
  •  醉话见心
    2021-02-07 12:14

    msg = 'Hello ' + (
        'there' if name is None else
        'Neo' if name == 'Mr Anderson' else
        name
    )
    

    This is a reiteration of several other answers, but with nicer formatting. I consider this most readable, and this is the approach I would use.

提交回复
热议问题