【问题标题】:PEP8-conform splitting of long f-string长 f 字符串的 PEP8-conform 拆分
【发布时间】:2020-07-25 23:28:50
【问题描述】:

如何将下面的长行分成两行,以符合PEP8?

percentage = f"{state[0] / state[1] * 100:{3 + (decimals > 0) + decimals}.{decimals}f}%"

注意: 这里的 f 字符串不能像previously asked question 接受的答案中所建议的那样简单地分成两个 f 字符串,因为这会破坏格式。因此,这里的这个问题需要一个不同的更通用的解决方案。

【问题讨论】:

  • 只需在“=”之后添加新行 -> percentage = \ f"{state[0] / state[1] * 100:{3 + (decimals > 0) + decimals}.{decimals}f}"
  • 如果您担心 88 个字符行上的 PEP8,只需计算上面一行中 f 字符串内的任何内容。请记住a foolish consistency is the hobgoblin of little minds

标签: python split pep8 f-string


【解决方案1】:

不要害怕使用变量!这将使您的代码更具可读性。

a = state[0] / state[1] * 100
b = 3 + (decimals > 0) + decimals
# of course, you would change the names here
percentage = f"{a:{b}.{decimals}f}%"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-09
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多