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

后端 未结 6 1760
天涯浪人
天涯浪人 2021-01-30 09:54

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

6条回答
  •  爱一瞬间的悲伤
    2021-01-30 10:41

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

    For example, there is plenty of code like this:

    kw1 = some_value
    kw2 = some_value
    kw3 = some_value
    some_func(
        1,
        2,
        kw1=kw1,
        kw2=kw2,
        kw3=kw3)
    

    As you see, it makes complete 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.

提交回复
热议问题