PEP8 79 char max

Tim Chase python.list at tim.thechases.com
Wed Jul 31 20:19:15 CEST 2013


On 2013-07-31 16:32, Grant Edwards wrote:
> On 2013-07-31, Tim Chase <python.list at tim.thechases.com> wrote:
> > I interpret Grant's statement as wanting the "table" to look like
> >
> >   for name, value, description in (
> >       ("cost",   42,   "How much it cost"),
> >       ("status", 3141, "Status code from ISO-3.14159"),
> >       ...
> >       ):
> >     do_something(name, value)
> >     print(description)
> 
> Exactly.  When you have more than about 5 columns and 10 rows,
> having things aligned makes it far, far, easier to maintain.

As mentioned by Marcelo, when they can vary in length, maintaining is
a pain.  Even if your editor supports aligning (like vim with Dr.
Chip's align.vim plugin).  If I have a table of greater dimensions, I
tend to refactor into a more readable+maintainable scheme, whether
using dicts or named tuples and then break out the rows onto their own
lines:

  from collections import namedtuple
  Descriptor = namedtuple("Descriptor", ["name", "value",
  "description"])
  for name, value, description in (
      Descriptor(
        name="cost",
        value=42,
        description="How much it cost",
        ),
      Descriptor(
        name="status",
        value=3141,
        description="Status code from ISO-3.14159",
        ),
      ):
    do_something(name, value)
    print(description)

Using a namedtuple, if you forget one of the fields (or add an
extra, or misspell one), it yells at you:

  TypeError: __new__() takes exactly 4 arguments (2 given)
  TypeError: __new__() takes exactly 4 arguments (6 given)
  TypeError: __new__() got an unexpected keyword argument 'nmae'

There is redundancy of the kwarg params, but this can be skipped
if you prefer DRY code to more readable code.

Doing this also has the benefit that, when diffing, you don't get
noise when columns are merely adjusted visually.

-tkc










More information about the Python-list mailing list