Define functions with too many arguments to abide by PEP8 standard

Solution 1:

An example is given in PEP 8:

class Rectangle(Blob):

    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):

So that is the official answer. Personally I detest this approach, in which continuation lines have leading whitespace that doesn't correspond to any real indentation level. My approach would be:

class Rectangle(Blob):

    def __init__(
        self, width, height,
        color='black', emphasis=None, highlight=0
    ):

. . . or just let the line run over 80 characters.

Solution 2:

For Python code that uses type annotations, I suggest this:

def some_func(
    foo: str,
    bar: str = 'default_string',
    qux: Optional[str] = None,
    qui: Optional[int] = None,
) -> List[str]:
    """
    This is an example function.
    """
    print(foo)
    ...

If you use yapf you can use these options in .style.yapf:

[style]
dedent_closing_brackets = true
split_arguments_when_comma_terminated = true

If you use black you don't need to do anything, it uses the above style out of the box, if you add a trailing comma behind the last parameter.