English 中文(简体)
PEP 8, 所以,在关键词论据或默认参数价值方面,没有环绕过面的空间?
原标题:PEP 8, why no spaces around = in keyword argument or a default parameter value?
  • 时间:2012-01-13 15:37:34
  •  标签:
  • python
  • pep8

为什么 8 建议在关键词或默认参数值上不要在<条码>上设置空间?

这是否与在《<条码>=的所有其他情况下建议空间不一致?

How is:

func(1, 2, very_long_variable_name=another_very_long_variable_name)

高于:

func(1, 2, very_long_variable_name = another_very_long_variable_name)

http://en.wikipedia.org/wiki/Benevolent_Dictator_for_Life” BDFL将受到赞赏。

这个问题比缺省值更严重,即刚刚从第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.

最佳回答

我认为,之所以如此,是因为关键词的争论基本上不同于变式转让。

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)

如你所看到的那样,把变数分配给一个关键词的论点完全一样,这样就能够提高可读性,使之没有空间。 更容易认识到,我们正在使用关键词,而不是给自己分配一个变量。

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.

问题回答

存在利弊。

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.

然而,有重大的好处。 如果遵守节奏规则,则只检索><>>>>>> 工具<>>>><>>>>/>>> 更为有效。

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 )

为此:

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

此外,将变数作为缺省值也没有什么意义。 也许有些不变的变量(实际上不变),在这种情况下,我将使用所有上限、描述性但尽可能短的名称。 ......

IMO leaving out the spaces for args provides cleaner visual grouping of the arg/value pairs; it looks less cluttered.

对我来说,它使法典更加可读,因此是一个好公约。

我认为,不同任务和关键词任务之间的风格方面的主要区别是,对于前者而言,只应有一个单独的<代码>=,而对于后者而言,通常有多个<代码><>>>>>。

如果没有其他考虑,我们宁愿foo = 42foo=42,因为后者不是典型的标识形式,而是因为以前的冰貌将变数和价值与白色空间分开。

但是,当一行有多项任务时,我们倾向于f(foo=42,条条令=43,baz=44)至<条码>,因为前者视而不见几个任务与白天空间,而后者则不难看出关键词/数值何在。

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.

我个人认为,无论方案拟定/标志语言如何,所有转让操作者在<代码>>>之前和之后的单一空间都应当是标准性的,因为有助于对不同渠道的标语进行眼睛区分。 (一) 将变数/参数名称从转让经营人打到象征性的<代码>=,从价值到象征性的/经常的表示值。

既不能阅读,也不能直觉把三条不同渠道的三条 to缩成一个单一的“参数——名称——发送者——价值/压力-图”。

例如,请考虑没有限定标记:

def my_func(par1: str, par2: str):
    print( %s %s  % (par1, par2))

cond =  conditional string 
my_func(par1= string with a lot of spaces ,
        par2=cond if cond is not None else  no string )

授予的数值为<代码>par2 也许应当储存在一个变量中,而不是作为“永久”表述。

par2 = cond if cond is not None else  no string 
my_func(par1= string with a lot of spaces , 
        par2=par2)

......然而,如果我们决定使用永恒的表述方式,我发现,在转让操作者之前和之后增加有限空间,使之更可读,几乎像一个字典物体(这块 p参数基本上有):

my_func(par1 =  string with a lot of spaces , 
        par2 = cond if cond is not None else  no string )
# OR
par2 = cond if cond is not None else  no string 
my_func(par1 =  string with a lot of spaces , 
        par2 = par2)

我认为,这有几个原因,尽管我可能只是理顺:

  1. It saves space, allowing more function definitions and calls to fit on one line and saving more space for the argument names themselves.
  2. 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.
  3. The syntax is then distinct from variable assignments, which may have the same name.
  4. Additionally, the syntax is (even more) distinct from equality checks a == b which can also be valid expressions inside a call.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...