Python 习语“... if ... else ...”表达

发布于 2024-08-27 04:17:40 字数 208 浏览 3 评论 0原文

如何写得更短:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚你发端 2024-09-03 04:17:40

表达式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 longer if: ... else: ... alternative.

我早已燃尽 2024-09-03 04:17:40

我会使用括号来使表达式更具可读性:

return ('%.0f' % float_var) if float_var else float_var

当我第一次看到它时,我认为这

return '%.0f' % (float_var if float_var else float_var)

很愚蠢。我必须尝试一下才能确定它的工作方式。

顺便说一句,您的第一个示例与第二个示例不同,

if float_var:
    return formatted_string
else:
    return None

这将始终返回格式化字符串或 None 。你的第一个例子,如果你传入任何计算结果为 False (False、0、0.0、""、[] 等)的值,将返回不变,因此你的返回类型可以是 string、boolean、list、int、float 等。这是可能不是您想要的,特别是如果 0.0 是 float_var 的合法值。我会将您的代码更改为:

return ('%.0f' % float_var) if isinstance(float_var, float) else None

或者:

try:
    return "%.0f" % float_var
except TypeError:
    return None

通过将它们转换为浮点数,它将适用于其他整数(和长整型)。

I would use brackets to make the expression more readable:

return ('%.0f' % float_var) if float_var else float_var

When I first saw it I read it as

return '%.0f' % (float_var if float_var else float_var)

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

if float_var:
    return formatted_string
else:
    return None

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:

return ('%.0f' % float_var) if isinstance(float_var, float) else None

alternatively:

try:
    return "%.0f" % float_var
except TypeError:
    return None

which will work for other integers (and longs) by converting them to float.

迷迭香的记忆 2024-09-03 04:17:40
  • 目前尚不清楚您到底想做什么。

    1. 最字面的解释就是这样工作

      <前><代码>>>>浮动变量 = 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 # 这是一个浮点数

      我几乎可以肯定你不想要这个。

    2. 我猜您想用“if float_var”检查None?如果是这样,您总是将其拼写为“if foo is not None”,而不是“if foo”,前者更清晰且不易出错。

      如果这您的意图,我建议您修改您的模型。通过重复返回 None 来传播错误是一件坏事:它很丑陋,容易出现错误,而且不符合习惯。请改用异常。

  • 越短并不总是越好。您的片段并不长得令人痛苦或难以处理。事实上,如果您使用它们来避免潜在的错误,您会希望将它们延长一点。

    • 有人可能建议为此滥用 的短路行为。但是,这会使代码更难阅读,并且不允许您在 None 和其他错误值之间进行指定,这通常会导致错误。
  • It is not clear what exactly you want to do.

    1. The most literal interpretation would have it work like this

      >>> float_var = 4.5
      >>> '%.0f' % float_var if float_var else float_var
      '5' # This is a string
      >>> float_var = 0.0
      >>> '%.0f' % float_var if float_var else float_var
      0.0 # This is a float
      

      which I am almost certain you did not want.

    2. 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.

    • Someone may suggest abusing the short-circuiting behavior of or for this. However, this makes code harder to read and doesn't allow you to specify between None and other false values, which often leads to bugs.
⊕婉儿 2024-09-03 04:17:40

如果您已经在使用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.

许一世地老天荒 2024-09-03 04:17:40
float_var and "%.0f" % float_vav

是不是很棒?

float_var and "%.0f" % float_vav

Isn't it awesome?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文