[Python-ideas] PEP8 dictionary indenting addition

Steven D'Aprano steve at pearwood.info
Sat Oct 8 20:25:29 EDT 2016


On Sat, Oct 08, 2016 at 09:26:13PM +0200, Jelte Fennema wrote:
> I have an idea to improve indenting guidelines for dictionaries for better
> readability: If a value in a dictionary literal is placed on a new line, it
> should have (or at least be allowed to have) a n additional hanging indent.
> 
> Below is an example:
> 
> mydict = {'mykey':
>               'a very very very very very long value',
>           'secondkey': 'a short value',
>           'thirdkey': 'a very very very '
>               'long value that continues on the next line',
> }

Looks good to me, except that my personal preference for the implicit 
string concatenation (thirdkey) is to move the space to the 
following line, and (if possible) align the parts:

mydict = {'mykey':
              'a very very very very very long value',
          'secondkey': 'a short value',
          'thirdkey': 'a very very very'
                      ' long value that continues on the next line',
          }

(And also align the closing brace with the opening brace.)

Really long lines like thirdkey are ugly no matter what you do, but I 
find that the leading space stands out more than the trailing space, and 
makes it more obvious that something out of the ordinary is going on. 
Very few string literals start with a leading space, so when I see one, 
I know to look more closely.

In your example, I find that I don't even notice the trailing space 
unless I read the string very carefully.


-- 
Steve


More information about the Python-ideas mailing list