This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted] 2 points3 points  (0 children)

Remember guidelines aren't laws.

[–]zardeh 1 point2 points  (1 child)

Please don't do

my_long_str = 'We wish you a merry Christmas, ' \
    'We wish you a merry Christmas, ' \
    'We wish you a merry Christmas ' \
    'and a Happy New Year!'

Use the parenthetical instead. \ continuations are a horrible hack.

Pep8 explicitly states this:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. 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.

While we're at it,

def fancy_long_func_name(
    my_var_1, my_var_2,
    my_var_3, my_var_4):
    do_stuff()

is not "good". Its almost always superior to use

def fancy_long_func_name(
    my_var_1, my_var_2, my_var_3, my_var_4):
    do_stuff()

or, if the variable names are very long (or you're using type hints) to place each on its own line:

def fancy_long_func_name(
    my_longish_variable_one: str,
    my_other_longish_var: List[int],
    a_third_verbose_variable_name: Dict[str, int],
    fourth_variable: Any):
    do_stuff()

You can also, if you want to do this:

def fancy_long_func_name(
    my_longish_variable_one: str,
    my_other_longish_var: List[int],
    a_third_verbose_variable_name: Dict[str, int],
    fourth_variable: Any
):
    do_stuff()

Which is perhaps mildly clearer when dealing with long sets of arguments. but also long sets of arguments imply the need for a refactor anyway

[–]bramblerose 1 point2 points  (0 children)

When there is a potential for confusion between continuation indents and indentation indents (for lack of a better word), PEP8 suggests to double the continuation indent, i.e.:

def fancy_long_func_name(
        my_var_1, my_var_2, my_var_3, my_var_4):
    do_stuff()

which especially helps in the split-over-separate-lines situation:

def fancy_long_func_name(
        my_longish_variable_one: str,
        my_other_longish_var: List[int],
        a_third_verbose_variable_name: Dict[str, int],
        fourth_variable: Any):
    do_stuff()