问题:PEP 8,为什么关键字参数或默认参数值的’=’周围没有空格?
为什么PEP 8建议不要=
在关键字参数或默认参数值中使用空格?
这是否与=
在Python代码中建议在每出现的其他地方推荐空格不一致?
怎么:
func(1, 2, very_long_variable_name=another_very_long_variable_name)
优于:
func(1, 2, very_long_variable_name = another_very_long_variable_name)
Python的BDFL与讨论/解释的任何链接将不胜感激。
请注意,这个问题更多的是关于kwargs而不是默认值,我只是使用了PEP 8中的措词。
我不是在征求意见。我要问这个决定背后的原因。这更像是在问我为什么要在C程序中{
与if
语句使用同一行,而不是是否应该使用它。
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.
回答 0
我猜这是因为关键字参数与变量赋值本质上是不同的。
例如,有很多这样的代码:
kw1 = some_value
kw2 = some_value
kw3 = some_value
some_func(
1,
2,
kw1=kw1,
kw2=kw2,
kw3=kw3)
如您所见,将变量分配给名称完全相同的关键字参数是完全有意义的,因此可以提高可读性,使它们看不到空格。更容易认识到我们正在使用关键字参数,而不是为其本身分配变量。
同样,参数往往在同一行中,而赋值通常每个都在各自的行中,因此节省空间可能是一个重要问题。
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.
回答 1
我不会使用very_long_variable_name作为默认参数。所以考虑一下:
func(1, 2, axis='x', angle=90, size=450, name='foo bar')
在此:
func(1, 2, axis = 'x', angle = 90, size = 450, name = 'foo bar')
同样,将变量用作默认值也没有多大意义。也许某些常量变量(实际上不是常量),在这种情况下,我将使用全为大写的名称,以描述性的方式表示,但尽可能简短。所以没有another_very _…
I wouldn’t use very_long_variable_name as a default argument. So consider this:
func(1, 2, axis='x', angle=90, size=450, name='foo bar')
over this:
func(1, 2, axis = 'x', angle = 90, size = 450, name = 'foo bar')
Also, it doesn’t make much sense to use variables as default values. Perhaps some constant variables (which aren’t really constants) and in that case I would use names that are all caps, descriptive yet short as possible. So no another_very_…
回答 2
有优点也有缺点。
我非常不喜欢PPE8兼容代码的读取方式。我不very_long_variable_name=another_very_long_variable_name
接受比人类更具可读性
的论点very_long_variable_name = another_very_long_variable_name
。人们不是这样阅读的。这是额外的认知负担,尤其是在没有语法突出显示的情况下。
但是,这有很大的好处。如果遵守间隔规则,则使使用工具专门搜索参数更加有效。
There are pros and cons.
I very much dislike how PEP8 compliant code reads. I don’t buy into the argument that very_long_variable_name=another_very_long_variable_name
can ever be more human readable than
very_long_variable_name = another_very_long_variable_name
.
This is not how people read. It’s an additional cognitive load, particularly in the absence of syntax highlighting.
There is a significant benefit, however. If the spacing rules are adhered to, it makes searching for parameters exclusively using tools much more effective.
回答 3
IMO省略了用于args的空间,从而提供了更清晰的arg / value对可视分组。看起来不那么混乱。
IMO leaving out the spaces for args provides cleaner visual grouping of the arg/value pairs; it looks less cluttered.
回答 4
我认为有几个原因,尽管我可能只是在进行合理化:
- 它节省了空间,允许更多的函数定义和调用适合一行,并为参数名称本身节省了更多空间。
- 通过将每个关键字和值连接在一起,可以更轻松地用逗号后的空格分隔不同的参数。这意味着您可以快速查看提供的参数数量。
- 这样,语法便与可能具有相同名称的变量分配不同。
- 此外,该语法(甚至更多)不同于相等检查
a == b
,相等检查也可以是调用内的有效表达式。
I think there are several reasons for this, although I might just be rationalizing:
- It saves space, allowing more function definitions and calls to fit on one line and saving more space for the argument names themselves.
- By joining each keyword and value, you can more easily separate the different arguments by the space after the comma. This means you can quickly eyeball how many arguments you’ve supplied.
- The syntax is then distinct from variable assignments, which may have the same name.
- Additionally, the syntax is (even more) distinct from equality checks
a == b
which can also be valid expressions inside a call.
回答 5
对我来说,它使代码更具可读性,因此是一个很好的约定。
我认为变量分配和函数关键字分配在样式方面的主要区别在于=
,前者在一行上应该只有一个,而通常有多个=
。
如果没有其他考虑,我们宁愿foo = 42
使用foo=42
,因为后者不是等号通常的格式,并且因为前者在视觉上很好地用空格分隔了变量和值。
但是,当有一行中的多个任务,我们更f(foo=42, bar=43, baz=44)
要f(foo = 42, bar = 43, baz = 44)
,因为前者在视觉上与分离空白的几个任务,而后者则没有,使得它有点难以看到的关键字/值对。
这里是把它的另一种方式:还有就是约定背后的一致性。一致性是这样的:通过空格使“最高分离级别”在视觉上更加清晰。较低级别的分隔不是(因为它将与分隔较高级别的空白混淆)。对于变量分配,最高的分隔级别是变量和值之间。对于功能关键字分配,最高的分隔级别是各个分配本身之间的分隔。
For me it makes code more readable and is thus a good convention.
I think the key difference in terms of style between variable assignments and function keyword assignments is that there should only be a single =
on a line for the former, whereas generally there are multiple =
s on a line for the latter.
If there were no other considerations, we would prefer foo = 42
to foo=42
, because the latter is not how equals signs are typically formatted, and because the former nicely visually separates the variable and value with whitespace.
But when there are multiple assignments on one line, we prefer f(foo=42, bar=43, baz=44)
to f(foo = 42, bar = 43, baz = 44)
, because the former visually separates the several assignments with whitespace, whereas the latter does not, making it a bit harder to see where the keyword/value pairs are.
Here’s another way of putting it: there is a consistency behind the convention. That consistency is this: the “highest level of separation” is made visually clearer via spaces. Any lower levels of separation are not (because it would be confused with the whitespace separating the higher level). For variable assignment, the highest level of separation is between variable and value. For function keyword assignment, the highest level of separation is between the individual assignments themselves.