python - 如何缩短长 "and"语句?

标签 python python-2.7 coding-style pep8

我有一个很长的“和”声明,例如:

if this == that and this == that and this == that:
    do this

如何正确地将此类语句分成单独的行,以符合 PEP-8?

最佳答案

PEP8 gives a recommendation :

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

和示例:

if (width == 0 and height == 0 and
    color == 'red' and emphasis == 'strong' or
    highlight > 100):
    raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
                                   emphasis is None):
    raise ValueError("I don't think so -- values are %s, %s" %
                     (width, height))

关于python - 如何缩短长 "and"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16830793/

相关文章:

python - 正确使用 GAE ndb.CompulatedProperty

python - 如何访问另一个对象内部的对象的方法

ios - 如何在 Xcode 和 AppCode 之间共享代码风格?

python - 以元组为值的字典?

python - flask 日志记录 - 无法将其写入文件

lua - 从 Lua 中的(最简单的)函数直接返回表条目

javascript - 复制参数对象的子集,不使用显式循环

java - 如何设置环境变量PATH以同时使用JAVA和Python

python - 不断收到 Kattis 问题的运行时错误

python - matplotlib:字符串的散点图