Search code examples
pythonpep8

Are there unwanted consequences to non-PEP8 compliant code (E211)?


Let's say there is a function defined with:

def thingy(arg1):
    pass

I know that the right way to call it, according to books and PEP8 (error code E211), is:

thingy(true)

However, I have also seen this following form in existing code:

thingy (true)

Apart from being against PEP8, can it be harmful?

In particular, can it create an unwanted bug/side-effect that would not be thrown using the recommended way?


Solution

  • No, whitespace is not significant, when used between a callable object an the call parentheses.

    From the lexical analysis reference documentation:

    Except at the beginning of a logical line or in string literals, the whitespace characters space, tab and formfeed can be used interchangeably to separate tokens. Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token (e.g., ab is one token, but a b is two tokens).

    and

    Besides NEWLINE, INDENT and DEDENT, the following categories of tokens exist: identifiers, keywords, literals, operators, and delimiters. Whitespace characters (other than line terminators, discussed earlier) are not tokens, but serve to delimit tokens. Where ambiguity exists, a token comprises the longest possible string that forms a legal token, when read from left to right.

    Emphasis mine.

    Where whitespace is not needed, it has no meaning other than to separate the tokens visually. thingy and ( are already two separate tokens, and won't be confused as one token when whitespace is absent. So the presence of whitespace here doesn't change the interpretation of the line.