Python “best formatting practice” for lists, dictionary, etc

白昼怎懂夜的黑 提交于 2019-11-27 14:24:33

问题


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?


回答1:


My preferred way is:

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



回答2:


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:

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



回答3:


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

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.




回答4:


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?




回答5:


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

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

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.




回答6:


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.




回答7:


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.



回答8:


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 ...




回答9:


I love the second way:

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



回答10:


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.




回答11:


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:"

my_dict = dict(
    foo = 1,
    bar = 2,
    baz = 3,
    ...
)

It doesn't settle the indentation question, however.



来源:https://stackoverflow.com/questions/3985563/python-best-formatting-practice-for-lists-dictionary-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!