在Python中格式化多行dict的正确方法是什么?

Rya*_*son 166 python code-formatting indentation multiline

在Python中,我想在我的代码中编写一个多行dict.有几种方法可以格式化它.以下是我能想到的一些内容:

  1. mydict = { "key1": 1,
               "key2": 2,
               "key3": 3, }
    
    Run Code Online (Sandbox Code Playgroud)
  2. mydict = { "key1": 1,
               "key2": 2,
               "key3": 3,
             }
    
    Run Code Online (Sandbox Code Playgroud)
  3. mydict = {
        "key1": 1,
        "key2": 2,
        "key3": 3,
    }
    
    Run Code Online (Sandbox Code Playgroud)

我知道上面的任何一个在语法上都是正确的,但我认为Python dicts有一个首选的缩进和换行样式.它是什么?

注意:这不是语法问题.以上所有(据我所知)有效的Python语句并且彼此等效.

Fog*_*ird 216

我使用#3.对于长列表,元组等也是如此.它不需要在缩进之外添加任何额外的空格.一如既往,保持一致.

mydict = {
    "key1": 1,
    "key2": 2,
    "key3": 3,
}

mylist = [
    (1, 'hello'),
    (2, 'world'),
]

nested = {
    a: [
        (1, 'a'),
        (2, 'b'),
    ],
    b: [
        (3, 'c'),
        (4, 'd'),
    ],
}
Run Code Online (Sandbox Code Playgroud)

类似地,这是我在不引入任何空格的情况下包含大字符串的首选方法(如果使用三引号多行字符串,则会得到):

data = (
    "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
    "l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
    "xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
    "rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh1"
    "AAAABJRU5ErkJggg=="
)
Run Code Online (Sandbox Code Playgroud)

  • 嗯,我发现了这个:http://stackoverflow.com/questions/6388187/what-is-the-proper-way-to-format-a-multi-line-dict-in-python/6388237#6388237 (73认同)
  • 不要告诉他,但用户不知道他在说什么; P (5认同)
  • 您能否提供一些参考资料,我很难找到这方面的权威来源。(我同意你的意见)。 (2认同)
  • 大声笑,更严重的是,我也找不到"权威"的参考.如果我这样做,我会告诉你的!也许有人应该联系Guido. (2认同)
  • 符合PEP 8:https://www.python.org/dev/peps/pep-0008/#indentation。在缩进部分的底部有一些列表示例。 (2认同)

Ray*_*Luo 26

首先,像Steven Rumbalski所说,"PEP8没有解决这个问题",所以这是个人偏好的问题.

我会使用与格式3类似但不完全相同的格式.这是我的,为什么.

my_dictionary = { # Don't think dict(...) notation has more readability
    "key1": 1, # Indent by one press of TAB (i.e. 4 spaces)
    "key2": 2, # Same indentation scale as above
    "key3": 3, # Keep this final comma, so that future addition won't show up as 2-lines change in code diff
    } # My favorite: SAME indentation AS ABOVE, to emphasize this bracket is still part of the above code block!
the_next_line_of_code() # Otherwise the previous line would look like the begin of this part of code

bad_example = {
               "foo": "bar", # Don't do this. Unnecessary indentation wastes screen space
               "hello": "world" # Don't do this. Omitting the comma is not good.
} # You see? This line visually "joins" the next line when in a glance
the_next_line_of_code()

btw_this_is_a_function_with_long_name_or_with_lots_of_parameters(
    foo='hello world',  # So I put one parameter per line
    bar=123,  # And yeah, this extra comma here is harmless too;
              # I bet not many people knew/tried this.
              # Oh did I just show you how to write
              # multiple-line inline comment here?
              # Basically, same indentation forms a natural paragraph.
    ) # Indentation here. Same idea as the long dict case.
the_next_line_of_code()

# By the way, now you see how I prefer inline comment to document the very line.
# I think this inline style is more compact.
# Otherwise you will need extra blank line to split the comment and its code from others.

some_normal_code()

# hi this function is blah blah
some_code_need_extra_explanation()

some_normal_code()
Run Code Online (Sandbox Code Playgroud)


dug*_*res 9

由于您的密钥是字符串,因为我们讨论的是可读性,所以我更喜欢:

mydict = dict(
    key1 = 1,
    key2 = 2,
    key3 = 3,
)
Run Code Online (Sandbox Code Playgroud)

  • 在定义kwargs时不希望使用空格.`c =函数(a = 1,b = 2)`更"pythonic". (6认同)