Python inline elif possible?

前端 未结 7 2248
耶瑟儿~
耶瑟儿~ 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 11:57

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

    I recommend against this; if your conditions become this complex, stick it in a function.

提交回复
热议问题