Allowing comments after the line continuation backslash

Roy Smith roy at
Tue Nov 2 12:46:23 CET 2010

In article <mailman.469.1288654964.2218.python-list at>,
 Chris Rebert <clp2 at> wrote:

> I find the level of deviation from PEP 8 in that file rather disturbing.
> In any case, the backslashes are easily avoided, and readability
> improved IMHO, via refactoring:
> desc_attr_colors_triples = (("normal", "image", MainWindow.ColorsNormalList),
>     ("highlighted", "highlight", MainWindow.ColorsHighlightedList),
>     ("selected", "select", MainWindow.ColorsSelectedList))
> for in description, attr, color_list in desc_attr_colors_triples:
>     ...

I like and use PEP-8.  At the start of any project involving myself, 
other people, and Python, I'll generally suggest we follow PEP-8 style, 
and I can't remember ever getting any pushback.  That being said, I 
don't hold it in awe.  Likewise, I don't worry in the least about 
deviating when readability would be improved by doing so.

In this case, I think I would do:

styles = [("normal",      "image",     MainWindow.ColorsNormalList),
          ("highlighted", "highlight", MainWindow.ColorsHighlightedList),
          ("selected",    "select",    MainWindow.ColorsSelectedList)]

for in description, attr, color_list in styles:
   blah, blah, blah

For those reading this in a non-fixed width font, I've laid out the 
definition of styles as a table, with spaces inserted to make the 
columns line up.  For data like this, I think it makes it easier to read 
and comprehend.  As a minor nit, note that I made it a list of tuples, 
not a tuple of tuples.

I'm tempted to do an additional refactoring to get rid of the verbose 
color list names:

CL_Normal = MainWindow.ColorsNormalList)
CL_Highlighted = MainWindow.ColorsHighlightedList
CL_Selected = MainWindow.ColorsSelectedList

styles = [("normal",      "image",     CL_Normal),
          ("highlighted", "highlight", CL_Highlighted),
          ("selected",    "select",    CL_Selected)]

I haven't decided if this makes things better or worse.  For this small 
table, I'm inclined to say worse.  If the table were much larger and I 
were reusing many of the color list names over and over, I would 
certainly do that.  If MainWindow were a well-designed module and I 
could do

import * from MainWindow

without cluttering up my namespace too much, I would do that, then just 
use the unadorned names.

Also, depending on what I was doing inside the loop, I might pick 
shorter names.  For example:

for in d, a, c in styles:

is perfectly clear.  Normally, I don't use single-letter variable names, 
but in this particular case, the descriptive function names provide all 
the context that's need to explain what they are.

More information about the Python-list mailing list