在 C# 中,我可以轻松编写以下内容:
string stringValue = string.IsNullOrEmpty( otherString ) ? defaultString : otherString;
在 Python 中是否有快速的方法来做同样的事情,或者我是否坚持使用“if”语句?
最佳答案
在 Python 2.5 中,有
A if C else B
它的行为很像 C 语言中的 ?:。但是,它不受欢迎的原因有两个:可读性,以及通常有更简单的方法来解决问题的事实。例如,在您的情况下:
stringValue = otherString or defaultString
关于python - 我怎样才能接近实现? : from C++/C# in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/135303/