如何打破 Python 中的一系列链式方法?
我有一行以下代码(不要责怪命名约定,它们不是我的):
subkeyword = Session.query(
Subkeyword.subkeyword_id, Subkeyword.subkeyword_word
).filter_by(
subkeyword_company_id=self.e_company_id
).filter_by(
subkeyword_word=subkeyword_word
).filter_by(
subkeyword_active=True
).one()
我不喜欢它的样子(不太可读),但我没有更好的想法来限制行数在这种情况下最多为 79 个字符。有没有更好的方法来打破它(最好没有反斜杠)?
I have a line of the following code (don't blame for naming conventions, they are not mine):
subkeyword = Session.query(
Subkeyword.subkeyword_id, Subkeyword.subkeyword_word
).filter_by(
subkeyword_company_id=self.e_company_id
).filter_by(
subkeyword_word=subkeyword_word
).filter_by(
subkeyword_active=True
).one()
I don't like how it looks like (not too readable), but I don't have any better idea to limit lines to 79 characters in this situation. Is there a better way of breaking it (preferably without backslashes)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用额外的括号:
You could use additional parentheses:
在这种情况下,行继续字符优先于左括号。随着方法名称变长以及方法开始接受参数,对这种风格的需求变得更加明显:
PEP 8 旨在以一定程度的常识和实用与美观的眼光来解释。很高兴违反任何 PEP 8 准则,从而导致代码丑陋或难以阅读。
话虽这么说,如果您经常发现自己与 PEP 8 不一致,这可能表明存在超出您选择的空白的可读性问题:-)
This is a case where a line continuation character is preferred to open parentheses. The need for this style becomes more obvious as method names get longer and as methods start taking arguments:
PEP 8 is intend to be interpreted with a measure of common-sense and an eye for both the practical and the beautiful. Happily violate any PEP 8 guideline that results in ugly or hard to read code.
That being said, if you frequently find yourself at odds with PEP 8, it may be a sign that there are readability issues that transcend your choice of whitespace :-)
我个人的选择是:
My personal choice would be:
只需存储中间结果/对象并调用其上的下一个方法,
例如
Just store the intermediate result/object and invoke the next method on it,
e.g.
它与其他人提供的解决方案有点不同,但这是我最喜欢的,因为它有时会导致漂亮的元编程。
这是构建搜索的好技术。通过条件列表从复杂的查询表单中挖掘(或基于字符串的关于用户正在查找的内容的推论),然后将字典分解到过滤器中。
It's a bit of a different solution than provided by others but a favorite of mine since it leads to nifty metaprogramming sometimes.
This is a nice technique for building searches. Go through a list of conditionals to mine from your complex query form (or string-based deductions about what the user is looking for), then just explode the dictionary into the filter.
根据 Python 语言参考
您可以使用反斜杠。
或者干脆打破它。如果括号未配对,Python 不会将其视为一行。在这种情况下,下面几行的缩进并不重要。
According to Python Language Reference
You can use a backslash.
Or simply break it. If a bracket is not paired, python will not treat that as a line. And under such circumstance, the indentation of following lines doesn't matter.
您似乎使用 SQLAlchemy,如果是真的, < code>sqlalchemy.orm.query.Query.filter_by() 方法需要多个关键字参数,所以你可以这样写:
但这会更好:
You seems using SQLAlchemy, if it is true,
sqlalchemy.orm.query.Query.filter_by()
method takes multiple keyword arguments, so you could write like:But it would be better:
顶部答案略有不同:主要对象(
Session
)保留在顶部行并使用单个缩进。这可以快速识别主对象和所有后续的链式方法调用。A slight variation of the top answer: the main object (
Session
) is kept in the top line and a single indentation is used. This enables quick identification of the main object and all subsequent chained method calls..我喜欢将参数缩进两块,将语句缩进一块,如下所示:
I like to indent the arguments by two blocks, and the statement by one block, like these: