列表、字典等的 Python“最佳格式实践”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3985563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:41:39  来源:igfitidea点击:

Python "best formatting practice" for lists, dictionary, etc

pythoncode-formatting

提问by Wizzard

I have been looking over the Pythondocumentation for code formatting best practice for large lists and dictionaries, for example,

我一直在查看Python文档,了解大型列表和字典的代码格式最佳实践,例如,

something = {'foo' : 'bar', 'foo2' : 'bar2', 'foo3' : 'bar3'..... 200 chars wide, etc..}

or

或者

something = {'foo' : 'bar',
             'foo2' : 'bar2',
             'foo3' : 'bar3',
             ...
             }

or

或者

something = {
             'foo' : 'bar',
             'foo2' : 'bar2',
             'foo3' : 'bar3',
             ...
             }

How do I handle deep nesting of lists/dictionaries?

如何处理列表/字典的深层嵌套?

采纳答案by aaronasterling

My preferred way is:

我的首选方式是:

something = {'foo': 'bar',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             'fooN': 'barN'}

回答by pyfunc

I prefer the second or third one.

我更喜欢第二个或第三个。

Reason:

原因:

  1. Each element is on its own line
  2. Reaching to end of line to add a new element is a pain in a text editor
  3. Adding a new element is easy
  4. With the third option, sometimes you can check the number of elements by selecting those lines. Most editors will tell you the number of selected lines.
  1. 每个元素都在自己的行上
  2. 到达行尾以添加新元素在文本编辑器中很痛苦
  3. 添加新元素很容易
  4. 使用第三个选项,有时您可以通过选择这些行来检查元素的数量。大多数编辑器会告诉您所选行的数量。

回答by Klaus Byskov Pedersen

Well, the first one is a no-go, since your lines should only 79 characters wide. With regards to the other two options, I suppose it's a matter of taste, but I personally prefer the second option.

嗯,第一个是不行的,因为你的行应该只有 79 个字符宽。至于其他两个选项,我想这是一个品味问题,但我个人更喜欢第二个选项。

回答by eumiro

Define your dictionary in any way you want and then try this:

以任何你想要的方式定义你的字典,然后试试这个:

from pprint import pprint

pprint(yourDict)

# for a short dictionary it returns:

{'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}

# for a longer/nested:

{'a00': {'b00': 0,
         'b01': 1,
         'b02': 2,
         'b03': 3,
         'b04': 4,
         'b05': 5,
         'b06': 6,
         'b07': 7,
         'b08': 8,
         'b09': 9},
 'a01': 1,
 'a02': 2,
 'a03': 3,
 'a04': 4,
 'a05': 5,
 'a06': 6,
 'a07': 7,
 'a08': 8,
 'a09': 9,
 'a10': 10}

Do you like the output?

你喜欢输出吗?

回答by ianaré

Definitely NOT option 1, one of the strenghts of Pythonis its legibility. Option 1 severely diminishes that legibility.

绝对不是选项 1,Python 的优势之一是它的易读性。选项 1 严重降低了可读性。

Out of 2 and 3, I'll echo the same reasons pyfunc stated for both.

在 2 和 3 中,我将回应 pyfunc 为两者陈述的相同原因。

However, in my own code I prefer option 3 simply because the first element sometimes gets 'lost' by being at the end of the declare line, and upon quick glancing at code sometimes I just won't see it immediately. I know it's a little silly, but the mind works in mysterious ways ...

然而,在我自己的代码中,我更喜欢选项 3,因为第一个元素有时会因为位于声明行的末尾而“丢失”,并且在快速浏览代码时有时我不会立即看到它。我知道这有点傻,但大脑以神秘的方式运作......

回答by Jungle Hunter

If you go by ganeti(which respects PEP 8) you should choose the third option.

如果您选择ganeti(尊重 PEP 8),您应该选择第三个选项。

something = {
             'foo1': 'bar1',
             'foo2': 'bar2',
             'foo3': 'bar3',
             ...
             }

I like this esp. because you can select onlythe elements you want. And I feel removing or adding elements to either ends is faster this way.

我喜欢这个 esp。因为你可以选择想要的元素。而且我觉得通过这种方式在两端移除或添加元素会更快。

Note:As pointed out in the comment there should be no whitespace before ':' (E203) as per PEP.

注意:正如评论中所指出的,根据 PEP,':' (E203) 之前不应该有空格。

回答by danlei

aaronasterling's indentation style is what I prefer. This, and several other styles are explained in another SO Question. Especially Lennart Regebro's answer gave a nice overview.

aaronasterling 的压痕风格是我比较喜欢的。这和其他几种样式在另一个 SO Question 中进行了解释。尤其是 Lennart Regebro 的回答给出了一个很好的概述。

But this style was the one most voted for:

但这种风格是投票最多的风格:

my_dictionary = {
    1: 'something',
    2: 'some other thing',
}

回答by kasx93

I love the second way:

我喜欢第二种方式:

something = {'foo' : 'bar',
         'foo2' : 'bar2',
         'foo3' : 'bar3',
         ...
         'fooN': 'barN'}

回答by jaydel

Previous to reading this post I would have opted for the third option you give. But now I might go for the one that is NOT T?r?k Gábor's style:

在阅读这篇文章之前,我会选择您提供的第三个选项。但现在我可能会选择不是 T?r?k Gábor 风格的:

my_dictionary = { 1: 'something', 2: 'some other thing', }

my_dictionary = { 1: '东西', 2: '其他东西', }

But honestly anything aside from your first option is probably fine.

但老实说,除了您的第一个选择之外,任何事情都可能没问题。

回答by frodopwns

According to the PEP8 style guidethere are two ways to format a dictionary:

根据PEP8 风格指南,有两种方法可以格式化字典:

mydict = {
    'key': 'value',
    'key': 'value',
    ...
    }

OR

或者

mydict = {
    'key': 'value',
    'key': 'value',
    ...
}

If you want to conform to PEP8 I would say anything else is technically wrong.

如果您想符合 PEP8,我会说其他任何事情在技术上都是错误的。