Python 习语“... if ... else ...”表达
如何写得更短:
return '%.0f' % float_var if float_var else float_var
or
if float_var:
return formatted_string
else:
return None
谢谢!
How to write the expression shorter:
return '%.0f' % float_var if float_var else float_var
or
if float_var:
return formatted_string
else:
return None
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
表达式
if <条件> else
已经非常惯用了——当然比其他示例更惯用,并且当
很简单时可能是首选。这是 Python 的三元运算符,因此如果您正在寻找类似的内容, ? <值> :
,不存在。如果计算
或
需要几个步骤,请使用较长的if: ... else: ...
选择。The expression
<value> if <condition> else <other_value>
is pretty idiomatic already -- certainly more so than the other example, and is probably preferred whenever<value>
is simple. This is Python's ternary operator, so if you were looking for something like<condition> ? <value> : <other_value>
, that doesn't exist.If computing
<value>
or<other_value>
takes a few steps, use the longerif: ... else: ...
alternative.我会使用括号来使表达式更具可读性:
当我第一次看到它时,我认为这
很愚蠢。我必须尝试一下才能确定它的工作方式。
顺便说一句,您的第一个示例与第二个示例不同,
这将始终返回格式化字符串或 None 。你的第一个例子,如果你传入任何计算结果为 False (False、0、0.0、""、[] 等)的值,将返回不变,因此你的返回类型可以是 string、boolean、list、int、float 等。这是可能不是您想要的,特别是如果 0.0 是 float_var 的合法值。我会将您的代码更改为:
或者:
通过将它们转换为浮点数,它将适用于其他整数(和长整型)。
I would use brackets to make the expression more readable:
When I first saw it I read it as
which would be silly. I had to try it out to make sure which way it worked.
BTW Your first example not equivalent to your second example
This will always return either a formatted string or None. Your first example if you pass in anything that evaluates to False (False, 0, 0.0, "", [] etc) will return that unchanged, so your return type could be string, boolean, list, int, float etc. This is probably not what you want, especially if 0.0 is a legitimate value for float_var. I would change your code to:
alternatively:
which will work for other integers (and longs) by converting them to float.
目前尚不清楚您到底想做什么。
最字面的解释就是这样工作
<前><代码>>>>浮动变量 = 4.5
>>>>> '%.0f' % float_var if float_var else float_var
'5' # 这是一个字符串
>>>>>浮动变量 = 0.0
>>>>> '%.0f' % float_var if float_var else float_var
0.0 # 这是一个浮点数
我几乎可以肯定你不想要这个。
我猜您想用“
if float_var
”检查None
?如果是这样,您总是将其拼写为“if foo is not None
”,而不是“if foo
”,前者更清晰且不易出错。如果这是您的意图,我建议您修改您的模型。通过重复返回
None
来传播错误是一件坏事:它很丑陋,容易出现错误,而且不符合习惯。请改用异常。越短并不总是越好。您的片段并不长得令人痛苦或难以处理。事实上,如果您使用它们来避免潜在的错误,您会希望将它们延长一点。
或
的短路行为。但是,这会使代码更难阅读,并且不允许您在None
和其他错误值之间进行指定,这通常会导致错误。It is not clear what exactly you want to do.
The most literal interpretation would have it work like this
which I am almost certain you did not want.
I guess you are wanting to check for
None
with "if float_var
"? If so, you always spell it "if foo is not None
", not "if foo
", the former being clearer and less bug-prone.If that is what you intended, I suggest you revise your model. Propagating errors by repeatedly returning
None
is a bad thing: it is ugly and bug-prone and non-idiomatic. Use exceptions instead.Shorter is not always better. Your snippets are not painfully long or unwieldly. In fact, you will want to make them a little longer if you use them to avoid a potential bug.
or
for this. However, this makes code harder to read and doesn't allow you to specify betweenNone
and other false values, which often leads to bugs.如果您已经在使用
v if c else u
,那么您已经在使用最具可读性和最高效的三元运算符。还有其他方法< /a> 但它们的可读性受到影响。
If you are using are already using
v if c else u
you are already using the most readable and efficient ternary operator.There are other ways but they suffer in readability.
是不是很棒?
Isn't it awesome?