soulcheck soulcheck - 6 months ago 13
Python Question

PEP 8, why no spaces around '=' in keyword argument or a default parameter value?

Why does PEP 8 recommend not having spaces around

in a keyword argument or a default parameter value?

Is this inconsistent with recommending spaces around every other occurrence of
=
in Python code?

How is:

func(1, 2, very_long_variable_name=another_very_long_variable_name)


better than:

func(1, 2, very_long_variable_name = another_very_long_variable_name)


Any links to discussion/explanation by Python's BDFL will be appreciated.

Mind, this question is more about kwargs than default values, i just used the phrasing from PEP 8.

I'm not soliciting opinions. I'm asking for reasons behind this decision. It's more like asking why would I use
{
on the same line as
if
statement in a C program, not whether I should use it or not.

Answer

I guess that it is because a keyword parameter is essentially different than a variable assignment.

For example, there is plenty of code like this:

kw1 = some calculation here
kw2 = some calculation here
kw3 = some calculation here
some_func(1,
    2,
    kw1=kw1,
    kw2=kw2,
    kw3=kw3)

As you see, it makes completely sense to assign a variable to a keyword argument named exactly the same, so it improves readability to see them without spaces. It is easier to recognize that we are using keyword arguments and not assigning a variable to itself.

Also, parameters tend to go in the same line whereas assignments usually are each one in their own line, so saving space is likely to be an important matter there.

Comments