Pythonic list/tuple/dict layout?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Jan 25 06:32:40 EST 2009


Akira Kitada <akitada at gmail.com> writes:

> I collected common layout from existing code and pasted them below.
> My vote would go to d1. How about yours?
> 
> If there is consensus on this, that might be worth being included in
> PEP 8.
> 
> Thanks,
> 
> """
> d1 = {
>     0: "ham",
>     1: "jam",
>     2: "spam",
>     3: "alot",
>     4: "knights",
>     5: "who",
>     6: "say",
>     7: "ni",
>     8: "dead",
>     9: "parrot",
> }
> 
> d2 = {
>     0: "ham",
>     1: "jam",
>     2: "spam",
>     3: "alot",
>     4: "knights",
>     5: "who",
>     6: "say",
>     7: "ni",
>     8: "dead",
>     9: "parrot",}

These are the only two that follow PEP 8; the others don't have
four-space indent levels.

I actually use this style:

    foo = {
        0: 'spam',
        1: 'eggs',
        2: 'beans',
        }

because that makes it clear that *all* the indented lines are a
continuation of the same statement, just like a suite of statements
are all uniformly indented under (e.g.) a function definition.

It seems that the author of the Python editing mode in Emacs agrees
with me too (the style, at least, if not the reasoning), which makes
my programming life easier.

-- 
 \       “The future always arrives too fast, and in the wrong order.” |
  `\                                                    —Alvin Toffler |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list