Line continuation and comments

Cameron Simpson cs at cskk.id.au
Wed Feb 22 18:59:18 EST 2023


On 22Feb2023 11:27, Thomas Passin <list1 at tompassin.net> wrote:
>On 2/22/2023 10:02 AM, Weatherby,Gerard wrote:
>>That’s a neat tip. End of line comments work, too
>>
>>x = (3 > 4  #never
>>      and 7 == 7  # hopefully
>>      or datetime.datetime.now().day > 15 # sometimes
>>      )
>>print(x)
>
>I find myself doing this more and more often.  It can also help to 
>make the code more readable and the intention more clear. Here's one 
>example:
>
>    return (getTerminalFromProcess()
>            or getTerminalFromDirectory('/usr/bin')
>            or getTerminalFromDirectory('/bin')
>            or getCommonTerminal(('konsole', 'xterm'))
>            )

Aye, me too.

I autoformat my code using `yapf` (I _hate_ `black`) and append my 
personal code style below. In particular, note the 
`split_before_logical_operator` which does the above automatically when 
it reflows a logical expression (you need the brackets, but you need 
them anyway for multiline stuff if you're eschewing the backslash).

     [style]
     based_on_style = pep8
     align_closing_bracket_with_visual_indent = True
     arithmetic_precedence_indication = False
     blank_line_before_module_docstring = True
     blank_line_before_nested_class_or_def = True
     blank_lines_around_top_level_definition = 1
     dedent_closing_brackets = True
     indent_dictionary_value = False
     indent_width = 2
     space_between_ending_comma_and_closing_bracket = False
     spaces_before_comment = 2
     split_before_dot = True
     split_before_expression_after_opening_paren = True
     split_before_first_argument = True
     split_before_logical_operator = True
     split_complex_comprehension = True
     use_tabs = False

So basicly PEP8 with some tweaks.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list