Pythonic list/tuple/dict layout?

A

Akira Kitada

Hi,

There is more than one way to write a list/tuple/dict in Python,
and actually different styles are used in standard library.
As a hobgoblin of little minds, I rather like to know which style is
considered "Pythonic"
in the community.

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",}

d3 = {0: "ham",
1: "jam",
2: "spam",
3: "alot",
4: "knights",
5: "who",
6: "say",
7: "ni",
8: "dead",
9: "parrot",
}

d4 = {0: "ham",
1: "jam",
2: "spam",
3: "alot",
4: "knights",
5: "who",
6: "say",
7: "ni",
8: "dead",
9: "parrot",}

d5 = {0: "ham",
1: "jam",
2: "spam",
3: "alot",
4: "knights",
5: "who",
6: "say",
7: "ni",
8: "dead",
9: "parrot",
}
"""
 
D

Dan Bishop

Hi,

There is more than one way to write a list/tuple/dict in Python,
and actually different styles are used in standard library.
As a hobgoblin of little minds, I rather like to know which style is
considered "Pythonic"
in the community.

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

}
[snip]

I use d1.

Wow! A Python debate over curly brace placement! Imagine that!
 
A

Akira Kitada

Wow! A Python debate over curly brace placement! Imagine that!

PEP8 even deals with tabs vs spaces, where to put a blank line, etc :)
 
S

Steven D'Aprano

Hi,

There is more than one way to write a list/tuple/dict in Python, and
actually different styles are used in standard library. As a hobgoblin
of little minds, I rather like to know which style is considered
"Pythonic"
in the community.

I collected common layout from existing code and pasted them below. My
vote would go to d1. How about yours?

All of them.

BTW, there's no need to use such large examples. Three items per dict
would be sufficient to illustrate the styles, using ten items doesn't add
anything useful to the discussion.
 
A

Akira Kitada

BTW, there's no need to use such large examples. Three items per dict
would be sufficient to illustrate the styles, using ten items doesn't add
anything useful to the discussion.

I worried to be told
'you can make it in a line like {"ham": "jam", "spam": "alot"}'
;)
 
A

Akira Kitada

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

In those examples, the following sentence in PEP 8 would be applied.

"Make sure to indent the continued line appropriately."
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.

Yes, it does, but people around me tend to prefer d1 style to that one.
One purpose of this silly thread is to figure out which is most popular one...
 
M

Mark Wooding

Akira Kitada said:
I collected common layout from existing code and pasted them below.
My vote would go to d1. How about yours?

It seems that I use both d1 and d4, though in both cases I omit the
trailing commas. I use d1 when each item is on a separate line, and d4
when I'm packing them onto multiple lines.

e.g.,

op = XT.make_optparse \
([('E', 'error',
{'action': 'store_const', 'dest': 'type', 'const': 'error',
'help': "Mark the window as reporting an error."}),
## ...
('t', 'title',
{'dest': 'title',
'help': "Set the window's title string."})],
version = VERSION,
usage = '%prog [-EIQWm] [-t TITLE] [-d HEADLINE] '
'MESSAGE [BUTTONS...]')

and

service_info = [('watch', T.VERSION, {
'adopted': (0, 0, '', cmd_adopted),
'kick': (1, 1, 'PEER', cmd_kick)
})]

In this latter case, were I defining multiple services, I'd indent it
differently:

service_info = [
('watch', T.VERSION, {
'adopted': (0, 0, '', cmd_adopted),
'kick': (1, 1, 'PEER', cmd_kick)
}),
##...
]

-- [mdw]
 
A

Aahz

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.

Ditto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,257
Members
46,857
Latest member
ArleenWill

Latest Threads

Top