PEP 8, why are there no spaces around the "=" in the keyword argument or default parameter value?

Why does PEP 8 recommend not having spaces around = in the keyword argument or default parameter value ?

Is this consistent with recommending spaces around any other = occurrence in Python code?

How to do it:

 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 discussion / explanation links using Python BDFL would be appreciated.

Mind, this question is more about signs than defaults, I just used the wording from PEP 8.

I am not asking for opinions. I ask for the reasons for this decision. This is more like asking why I would use { on the same line as the if in a C program, and not , should I use it or not. p>

+84
python coding-style pep8
Jan 13 '12 at 15:37
source share
6 answers

I assume this is because the keyword argument is significantly different from variable assignment.

For example, there are many such codes:

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

As you can see, it is absolutely necessary to assign a variable to a keyword argument with a name in the same way, so it improves readability so that you can see them without spaces. It’s easier to recognize that we use keyword arguments and do not assign a variable to ourselves.

In addition, the parameters usually go on one line, while the assignments are usually separate on their line, so saving space is likely to be an important issue there.

+61
Jan 13 2018-12-01T00:
source share

I would not use very_long_variable_name as the default argument. Therefore, consider the following:

 func(1, 2, axis='x', angle=90, size=450, name='foo bar') 

above this:

 func(1, 2, axis = 'x', angle = 90, size = 450, name = 'foo bar') 

In addition, it makes no sense to use variables as default values. Perhaps some constant variables (which are not actually constants), in which case I will use names that are all caps, but descriptions, although as short as possible. So no another_very _...

+11
Jan 13 '12 at 15:43
source share

IMO, leaving spaces for args, provides a clearer visual grouping of arg / value pairs; he looks less cluttered.

+7
Mar 26 '14 at 21:00
source share

There are pros and cons.

I really don't like the way PEP8-compatible code is read. I do not agree that very_long_variable_name=another_very_long_variable_name may be more understandable to a person than very_long_variable_name = another_very_long_variable_name . This is not how people read. This is an additional cognitive load, especially in the absence of syntax highlighting.

However, there are significant benefits. If the rules of intervals are observed, this significantly increases the efficiency of searching for parameters using tools .

+7
May 25 '16 at 10:12
source share

I think there are several reasons for this, although I can just rationalize:

  • This saves space by allowing you to define and define function definitions and maintain them on the same line and save space for the argument names themselves.
  • By combining each keyword and value, you can more easily separate the different arguments with a space after the decimal point. This means that you can quickly determine the number of arguments you have provided.
  • The syntax is then different from variable assignments, which may have the same name.
  • In addition, the syntax (even more) is different from equality checks a == b , which can also be valid expressions inside a call.
+3
Jun 08 '14 at 7:17
source share

For me, this makes the code more readable and thus a good convention.

I think that the key difference in terms of style between variable assignments and function keyword assignments is that for the former there should be only one = in a string, while in the general case for the latter, several = .

If there were no other considerations, we would prefer foo = 42 to foo=42 , since the latter is not the same as the equal sign, as a rule, is formatted, and because the former clearly visually separates the variable and value with spaces.

But when there are several assignments in one line, we prefer f(foo=42, bar=43, baz=44) f(foo = 42, bar = 43, baz = 44) , since the former visually separates several assignments. with spaces, while the latter doesn't, making it a little harder to see where the keyword / value pairs are.

Here is another way to express this: there is consistency behind the agreement. This sequence is as follows: the "highest level of separation" is visually clarified through spaces. There are no lower levels of separation (because it will be confused with spaces separating the higher level). To assign a variable, the highest level of separation is between the variable and the value. To assign function keywords, the highest level of separation is between the individual assignments themselves.

0
Jul 03 '19 at 21:48
source share



All Articles