What is the proper way to format a multi-line dict in Python?

后端 未结 7 1467
天命终不由人
天命终不由人 2020-11-29 16:19

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:

  1. mydic         
    
    
            
相关标签:
7条回答
  • 2020-11-29 16:23

    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.

    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()
    
    0 讨论(0)
  • 2020-11-29 16:36

    I use #3. Same for long lists, tuples, etc. It doesn't require adding any extra spaces beyond the indentations. As always, be consistent.

    mydict = {
        "key1": 1,
        "key2": 2,
        "key3": 3,
    }
    
    mylist = [
        (1, 'hello'),
        (2, 'world'),
    ]
    
    nested = {
        a: [
            (1, 'a'),
            (2, 'b'),
        ],
        b: [
            (3, 'c'),
            (4, 'd'),
        ],
    }
    

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

    data = (
        "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
        "l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
        "xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
        "rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh1"
        "AAAABJRU5ErkJggg=="
    )
    
    0 讨论(0)
  • 2020-11-29 16:36

    Generally, you would not include the comma after the final entry, but Python will correct that for you.

    0 讨论(0)
  • 2020-11-29 16:42

    From my experience with tutorials, and other things number 2 always seems preferred, but it's a personal preference choice more than anything else.

    0 讨论(0)
  • 2020-11-29 16:43

    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.

    0 讨论(0)
  • 2020-11-29 16:47

    Since your keys are strings and since we are talking about readability, I prefer :

    mydict = dict(
        key1 = 1,
        key2 = 2,
        key3 = 3,
    )
    
    0 讨论(0)
提交回复
热议问题