Skip to content

collections multiline

stv edited this page Jul 12, 2018 · 1 revision

Collections: Multiline

Expectation

  • Define collections (lists, dictionaries, sets) with one item per line
  • Indent lines by one level
    • don't align with the opening symbol: (/[/{

Example

Good

words = [
    'foo',
    'bar',
]

Bad

words = ['foo', 'bar',]
words = ['foo',
         'bar',]

Explanation

Multiline collections are easier to manage (insert/remove) when each item is on its own line.

This also reduces long lines, making the code easier to read.

Consider:

     'foo',
-    'bar',
 ]
     'bar',
+    'baz',
 ]

versus:

- words = ['foo', 'bar',]
+ words = ['foo',]
- words = ['foo', 'bar',]
+ words = ['foo', 'bar', 'baz',]

External Resources:

  1. http://edx.readthedocs.io/projects/edx-developer-guide/en/latest/style_guides/python_guidelines.html#breaking-long-lines
Clone this wiki locally