Python inline elif possible?

前端 未结 7 2243
耶瑟儿~
耶瑟儿~ 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

    Use a dictionary to perform a mapping:

    srepr = "'Modify " + {"p": "Pointer", "v": "value"}.get(self.register, "Unknown")
    

    (by the way, instead of '\'...' you can use "'... for a bit more clarity.

提交回复
热议问题