我如何分解这条线以满足PEP8要求?

时间:2017-07-27 05:44:37

标签: python python-2.7 sqlalchemy pep8 pep

我收到了太长的错误'在PEP8在线检查中的“'类别'属性。我的代码如下所示:

class A:
    __tablename__ = 'items'

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

3 个答案:

答案 0 :(得分:2)

Shai的回答(在第一次arg和缩进后打开paren)是好的,并且符合PEP8,但是如果你怀疑在以后的重构中关系函数可能有更多的参数:

class A:
    __tablename__ = 'items'

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

答案 1 :(得分:1)

怎么样

class A:
    __tablename__ = 'items'

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

答案 2 :(得分:1)

PEP8说:

  

包装长行的首选方法是在括号,括号和大括号内使用Python隐含的行继续。

关于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')
)

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