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

前端 未结 9 1915
滥情空心
滥情空心 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条回答
  •  太阳男子
    2021-02-20 11:40

    In Python 2.5, there is

    A if C else B
    

    which behaves a lot like ?: in C. However, it's frowned upon for two reasons: readability, and the fact that there's usually a simpler way to approach the problem. For instance, in your case:

    stringValue = otherString or defaultString
    

提交回复
热议问题