How can I closely achieve ?: from C++/C# in Python?

前端 未结 9 1944
滥情空心
滥情空心 2021-02-20 11:02

In C# I could easily write the following:

string stringValue = string.IsNullOrEmpty( otherString ) ? defaultString : otherString;

Is there a qu

9条回答
  •  萌比男神i
    2021-02-20 11:20

    It's never a bad thing to write readable, expressive code.

    if otherString:
       stringValue = otherString
    else:
       stringValue = defaultString
    

    This type of code is longer and more expressive, but also more readable and less likely to get tripped over or mis-edited down the road. Don't be afraid to write expressively - readable code should be a goal, not a byproduct.

提交回复
热议问题