Python“最佳格式化实践”用于列表、字典等
我一直在查看 Python 文档,了解大型列表的代码格式化最佳实践和例如字典,
something = {'foo' : 'bar', 'foo2' : 'bar2', 'foo3' : 'bar3'..... 200 chars wide, etc..}
或者
something = {'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
}
如何
something = {
'foo' : 'bar',
'foo2' : 'bar2',
'foo3' : 'bar3',
...
}
处理列表/字典的深度嵌套?
I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example,
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
根据PEP8风格指南,有两种格式化字典的方法:
或者
如果你想要为了符合 PEP8,我想说任何其他内容在技术上都是错误的。
According to the PEP8 style guide there are two ways to format a dictionary:
OR
If you want to conform to PEP8 I would say anything else is technically wrong.
我首选的方式是:
My preferred way is:
aaronasterling 的缩进风格是我喜欢的。这个以及其他几种样式在另一个SO问题中进行了解释。尤其是 Lennart Regebro 的回答给出了很好的概述。
但这种风格是投票最多的一种:
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.
But this style was the one most voted for:
以您想要的任何方式定义您的字典,然后尝试以下操作:
您喜欢输出吗?
Define your dictionary in any way you want and then try this:
Do you like the output?
如果您使用 ganeti(尊重 PEP 8),您应该选择第三个选项。
我喜欢这个,特别是。因为您可以仅选择您想要的元素。我觉得这样在两端删除或添加元素会更快。
注意: 正如评论中所指出的,根据 PEP,“:”(E203) 之前不应有空格。
If you go by ganeti (which respects PEP 8) you should choose the third option.
I like this esp. because you can select only the elements you want. And I feel removing or adding elements to either ends is faster this way.
Note: As pointed out in the comment there should be no whitespace before ':' (E203) as per PEP.
绝对不是选项 1,Python 的优点之一是它的易读性。选项 1 严重降低了可读性。
在 2 和 3 中,我将回应 pyfunc 为两者陈述的相同原因。
然而,在我自己的代码中,我更喜欢选项 3,因为第一个元素有时会因位于声明行末尾而“丢失”,并且在快速浏览代码时,有时我不会立即看到它。我知道这有点傻,但大脑以神秘的方式运作......
Definitely NOT option 1, one of the strenghts of Python is its legibility. Option 1 severely diminishes that legibility.
Out of 2 and 3, I'll echo the same reasons pyfunc stated for both.
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 ...
好吧,第一个是不行的,因为你的行应该只有 79 个字符宽。至于其他两个选项,我认为这是一个品味问题,但我个人更喜欢第二个选项。
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.
我更喜欢第二个或第三个。
原因:
I prefer the second or third one.
Reason:
在阅读这篇文章之前,我会选择您提供的第三个选项。但现在我可能会选择一种不是 Török Gábor 风格的:
my_dictionary = {
1:“某事”,
2:“其他一些事情”,
但
老实说,除了你的第一个选择之外的任何东西都可能没问题。
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:
my_dictionary = {
1: 'something',
2: 'some other thing',
}
But honestly anything aside from your first option is probably fine.
我喜欢第二种方式:
I love the second way:
我想提一下以下选项,该选项在 PEP8 中没有具体提及,但在 字典文档:“当键是简单字符串时,有时使用关键字参数指定对会更容易:”
但是,它并没有解决缩进问题。
I want to mention the following option, which is not specifically mentioned in the PEP8, but is noted in the dictionary documentation: "When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:"
It doesn't settle the indentation question, however.