As a rule, I try to break the first point in the "dominant" syntactic connective and use one indentation for the continuation line for the normal operator, or double indentation if the polyline is followed by a colon. However, if the syntax is "." then I prefer to use temporary variables, as this is usually more clear.
For your examples:
self.proc.stdin.write('(SayText "%s")\n' % text.replace('\\', '\\\\').replace('"', '\\"'))
I would write:
self.proc.stdin.write( '(SayText "%s")\n' % text.replace('\\', '\\\\').replace('"', '\\"'))
For
filename = tkFileDialog.askopenfilename(filetypes = (("Word list", "*.tldr"), ("All files", "*.*")))
I would write:
filename = tkFileDialog.askopenfilename( filetypes = (("Word list", "*.tldr"), ("All files", "*.*")))
For function calls with many arguments, I sometimes find it most clear to put one on a line, even if more can fit. For instance.
filename = some_function_call_with_long_args( the_first_argument = some_rather_long_expression, another_argument = some_other_expression )
It would be:
filename = some_function_call_with_long_args( the_first_argument = some_rather_long_expression, another_argument = some_other_expression )
or even:
filename = some_function_call_with_long_args( the_first_argument = some_rather_long_expression, another_argument = some_other_expression )
A variant of this is shown for statements with final completion:
for foo in this_is_a_long_function_generating_an_iterable( here_are_some = arguments, and_they = are_long_too ): print foo
becomes:
for foo in this_is_a_long_function_generating_an_iterable( here_are_some = arguments, and_they = are_long_too ): print foo
but usually it will be clearer
foo_iter = this_is_a_long_function_generating_an_iterable( here_are_some = arguments, and_they = are_long_too ) for foo in foo_iter: print foo
or
foo_iter = this_is_a_long_function_generating_an_iterable( here_are_some = arguments, and_they = are_long_too ) for foo in foo_iter: print foo
One final note: some people find these rules obsolete since you can usually use a larger window. I believe that these rules are very useful because:
- with successively short lines, I can see the code in more open windows (or more editing panels)
- the method above shows the logical structure of the program
- when it is not easy to break the lines, this is often a sign that the structure will be better displayed with temporary variables (etc.).