如何在Python中从C ++ / C#紧密实现? [英] How can I closely achieve ?: from C++/C# in Python?

查看:43
本文介绍了如何在Python中从C ++ / C#紧密实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我可以轻松地编写以下内容:

In C# I could easily write the following:

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

在Python中是否可以快速执行相同的操作,或者我是否坚持使用'if'

Is there a quick way of doing the same thing in Python or am I stuck with an 'if' statement?

推荐答案

在Python 2.5中,有

In Python 2.5, there is

A if C else B

在C中的行为很像?:但是,它不赞成这样做有两个原因:可读性和通常有一种更简单的方法来解决问题。例如,在您的情况下:

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

这篇关于如何在Python中从C ++ / C#紧密实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆