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

前端 未结 9 1904
滥情空心
滥情空心 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:44

    There are a few duplicates of this question, e.g.

    • Does Python have a ternary conditional operator?
    • What's the best way to replace the ternary operator in Python?

    In essence, in a general setting pre-2.5 code should use this:

     (condExp and [thenExp] or [elseExp])[0]
    

    (given condExp, thenExp and elseExp are arbitrary expressions), as it avoids wrong results if thenExp evaluates to boolean False, while maintaining short-circuit evaluation.

    0 讨论(0)
  • 2021-02-20 11:45

    By the way, j0rd4n, you don't (please don't!) write code like this in C#. Apart from the fact that the IsDefaultOrNull is actually called IsNullOrEmpty, this is pure code bloat. C# offers the coalesce operator for situations like these:

    string stringValue = otherString ?? defaultString;
    

    It's true that this only works if otherString is null (rather than empty) but if this can be ensured beforehand (and often it can) it makes the code much more readable.

    0 讨论(0)
  • 2021-02-20 11:46

    @Dan

    if otherString:
       stringValue = otherString
    else:
       stringValue = defaultString
    

    This type of code is longer and more expressive, but also more readable

    Well yes, it's longer. Not so sure about “more expressive” and “more readable”. At the very least, your claim is disputable. I would even go as far as saying it's downright wrong, for two reasons.

    First, your code emphasizes the decision-making (rather extremely). Onthe other hand, the conditional operator emphasizes something else, namely the value (resp. the assignment of said value). And this is exactly what the writer of this code wants. The decision-making is really rather a by-product of the code. The important part here is the assignment operation. Your code hides this assignment in a lot of syntactic noise: the branching.

    Your code is less expressive because it shifts the emphasis from the important part.

    Even then your code would probably trump some obscure ASCII art like ?:. An inline-if would be preferable. Personally, I don't like the variant introduced with Python 2.5 because it's backwards. I would prefer something that reads in the same flow (direction) as the C ternary operator but uses words instead of ASCII characters:

    C = if cond then A else B
    

    This wins hands down.

    C and C# unfortunately don't have such an expressive statement. But (and this is the second argument), the ternary conditional operator of C languages is so long established that it has become an idiom in itself. The ternary operator is as much part of the language as the “conventional” if statement. Because it's an idiom, anybody who knows the language immediately reads this code right. Furthermore, it's an extremely short, concise way of expressing these semantics. In fact, it's the shortest imaginable way. It's extremely expressive because it doesn't obscure the essence with needless noise.

    Finally, Jeff Atwood has written the perfect conclusion to this: The best code is no code at all.

    0 讨论(0)
提交回复
热议问题