Python style for `chained` function calls

Viewed 5150

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. 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 (\).

To quote from the Python style guide:

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.

3 Answers
Related