简体   繁体   English

用于`chained`函数调用的Python样式

[英]Python style for `chained` function calls

More and more we use chained function calls: 我们越来越多地使用链式函数调用:

value = get_row_data(original_parameters).refine_data(leval=3).transfer_to_style_c()

It can be long. 它可能很长。 To save long line in code, which is prefered? 要保存代码中的长行,这是首选?

value = get_row_data(
    original_parameters).refine_data(
    leval=3).transfer_to_style_c()

or: 要么:

value = get_row_data(original_parameters)\
       .refine_data(leval=3)\
       .transfer_to_style_c()

I feel it good to use backslash \\ , and put .function to new line. 我觉得使用反斜杠\\很好,并将.function放到新行。 This makes each function call has it own line, it's easy to read. 这使得每个函数调用都有自己的行,它很容易阅读。 But this sounds not preferred by many. 但这听起来不是很多人喜欢的。 And when code makes subtle errors, when it's hard to debug, I always start to worry it might be a space or something after the backslash (\\) . 当代码发生微妙的错误时,当它很难调试时,我总是开始担心它可能是一个空格或backslash (\\)后面的东西backslash (\\)

To quote from the Python style guide: 引用Python样式指南:

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. 打破二元运算符的首选位置是运算符之后,而不是它之前。

I tend to prefer the following, which eschews the non-recommended \\ , thanks to an opening parenthesis: 我倾向于选择以下内容,因为一个左括号,它避开了非推荐的 \\

value = (get_row_data(original_parameters)
         .refine_data(level=3)
         .transfer_to_style_c())

One advantage of this syntax is that each method call is on its own line. 此语法的一个优点是每个方法调用都在其自己的行上。

A similar kind of \\ -less structure is also often useful with string literals, so that they don't go beyond the recommended 79 character per line limit: 类似的\\ -less结构对于字符串文字通常也很有用,因此它们不会超出每行限制推荐的79个字符:

message = ("This is a very long"
           " one-line message put on many"
           " source lines.")

This is a single string literal, which is created efficiently by the Python interpreter (this is much better than summing strings, which creates multiple strings in memory and copies them multiple times until the final string is obtained). 这是一个字符串文字,由Python解释器有效创建(这比求和字符串要好得多,字符串在内存中创建多个字符串并多次复制它们直到获得最终字符串)。

Python's code formatting is nice. Python的代码格式好的。

What about this option: 这个选项怎么样:

value = get_row_data(original_parameters,
            ).refine_data(leval=3,
                ).transfer_to_style_c()

Note that commas are redundant if there are no other parameters but I keep them to maintain consistency. 请注意,如果没有其他参数,则逗号是多余的,但我保留它们以保持一致性。

The not quoting my own preference (although see comments on your question:)) or alternatives answer to this is: 不引用我自己的偏好(虽然看到你的问题的评论:))或替代答案是:

Stick to the style guidelines on any project you have already - if not stated, then keep as consistent as you can with the rest of the code base in style. 坚持你已经完成的任何项目的风格指导 - 如果没有说明,那么尽可能保持与代码库的其余部分一致。

Otherwise, pick a style you like and stick with that - and let others know somehow that's how you'd appreciate chained function calls to be written if not reasonably readable on one-line (or however you wish to describe it). 否则,选择一个你喜欢的风格并坚持下去 - 让其他人知道你是如何理解链接函数调用如果在一行上没有合理的可读性(或者你想描述它)的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM