Python style for `chained` function calls Python style for `chained` function calls python python

Python style for `chained` function calls


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:

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's code formatting is nice.


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