我怎样才能打破这一行来满足 PEP8 要求?

How can i break up this line to meet PEP8 requirements?

我在 PEP8 在线检查器中得到 'Line too long error' 'category' 属性。我的一段代码如图:

class A:
    __tablename__ = 'items'

    category = relationship(Category, backref=backref('items', cascade='all, delete'))
    id = Column(Integer, primary_key=True)

怎么样

class A:
    __tablename__ = 'items'

    category = relationship(Category, 
                            backref=backref('items', cascade='all, delete'))
    id = Column(Integer, primary_key=True)

Shai 的回答(在第一个 arg 之后中断并缩进到 open paren)很好,并且适合 PEP8,但是如果您怀疑在以后的重构中关系函数可能会有更多参数:

class A:
    __tablename__ = 'items'

    category = relationship(
        Category,
        backref=backref('items', cascade='all, delete'),
    )
    id = Column(Integer, primary_key=True)

PEP8 说:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces.

Indentation 部分给出了一些可能性的例子。具体怎么做取决于你的口味。

我最喜欢的是:

# if you need to save lines:
category = relationship(Category,
                        backref=backref('items', cascade='all, delete'))

# if you need it more structured:
category = relationship(
    Category, backref=backref('items', cascade='all, delete')
)

# if you have space and want a good overview:
category = relationship(
    Category,
    backref=backref('items', cascade='all, delete')
)

我个人最常使用最后一个选项,因为它在视觉上与代码的嵌套结构相对应。