在Python中格式化多行字典的正确方法是什么?
在Python中,我想在我的代码中编写一个多行字典。有几种方法可以对其进行格式化。以下是我能想到的一些:
mydict = { "key1": 1, “键2”:2, “键3”:3,}
mydict = { "key1": 1, “键2”:2, “键3”:3, }
mydict = { “键1”:1, “键2”:2, “键3”:3, }
我知道上述任何一种在语法上都是正确的,但我假设 Python 字典有一种首选的缩进和换行样式。它是什么?
注意:这不是语法问题。以上所有内容(据我所知)都是有效的 Python 语句,并且彼此等效。
In Python, I want to write a multi-line dict in my code. There are a couple of ways one could format it. Here are a few that I could think of:
mydict = { "key1": 1, "key2": 2, "key3": 3, }
mydict = { "key1": 1, "key2": 2, "key3": 3, }
mydict = { "key1": 1, "key2": 2, "key3": 3, }
I know that any of the above is syntactically correct, but I assume that there is one preferred indentation and line-break style for Python dicts. What is it?
Note: This is not an issue of syntax. All of the above are (as far as I know) valid Python statements and are equivalent to each other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我用#3。对于长列表、元组等也是如此。它不需要在缩进之外添加任何额外的空格。一如既往,保持一致。
同样,这是我在不引入任何空格的情况下包含大字符串的首选方法(就像使用三引号多行字符串时所得到的那样):
I use #3. Same for long lists, tuples, etc. It doesn't require adding any extra spaces beyond the indentations. As always, be consistent.
Similarly, here's my preferred way of including large strings without introducing any whitespace (like you'd get if you used triple-quoted multi-line strings):
首先,正如 Steven Rumbalski 所说,“PEP8 没有解决这个问题”,所以这是个人喜好的问题。
我会使用与您的格式 3 类似但不相同的格式。这是我的格式,以及原因。
First of all, like Steven Rumbalski said, "PEP8 doesn't address this question", so it is a matter of personal preference.
I would use a similar but not identical format as your format 3. Here is mine, and why.
由于您的键是字符串,并且我们正在讨论可读性,因此我更喜欢:
Since your keys are strings and since we are talking about readability, I prefer :
flake8 – 一个用于在 python 代码中强制风格一致性的实用程序,它检查代码语法并提供改进它的说明 – 推荐这种格式(请参阅 https://www.flake8rules.com/rules/E133.html):
flake8 – a utility for enforcing style consistency in python code, which checks your code syntax and provide instructions to improve it – recommends this format (see https://www.flake8rules.com/rules/E133.html):
通常,如果你有大的 python 对象,那么格式化它们是相当困难的。我个人更喜欢使用一些工具来实现这一点。
这是python-beautifier - www.cleancss.com/python-beautify,它可以立即转变您的数据进入可定制的风格。
Usually, if you have big python objects it's quite hard to format them. I personally prefer using some tools for that.
Here is python-beautifier - www.cleancss.com/python-beautify that instantly turns your data into customizable style.
如果您配置了 Black 格式化程序,它将是一行短文本字典:
当我想手动覆盖 行长配置并将字典格式设置为多行,只需在字典中添加注释即可:
黑色将使字典保持多行。
If you have configured the Black formatter, it will one-line short dictionaries:
When I want to manually override the line-length configuration and format the dictionary as multi-line, just add a comment in your dictionary:
and Black will keep the dictionary multi-line.
根据我在教程和其他方面的经验,第二个似乎总是首选,但这是个人偏好的选择。
From my experience with tutorials, and other things number 2 always seems preferred, but it's a personal preference choice more than anything else.
通常,您不会在最终条目后包含逗号,但 Python 会为您更正它。
Generally, you would not include the comma after the final entry, but Python will correct that for you.