[Python-ideas] Another indentation style

Stephen J. Turnbull stephen at xemacs.org
Mon Sep 2 05:39:33 CEST 2013

Steven D'Aprano writes:

 > def test(condition, a, b):
 >      if condition=='equals':
 >          return a==b
 >      if condition=='is greater than':
 >          return a>b
 >      if condition=='contains':
 >          return b in a
 >      if condition=='starts with':
 >          return a.startswith(b)
 > You can run your eye down indent level 2 and see "condition return
 > condition return condition return", which avoids needing to scan
 > left-to-right

No, you can't, FVO "you" == "me".  In fact, I can only read about 20
characters without moving my eyes, and your format encourages my eyes
to zigzag.  I find it *much* easier to scan the OP's format for a
particular condition or a particular return expression.

 > Another issue is the time spent deleting and inserting spaces in
 > the middle of the lines to keep the return statements lined up
 > after edits.

"Obviously" you're not an Emacs user (or other editor with powerful
on-the-fly scripting capability).  It takes about 10 minutes to write
enough Lisp to maintain that table with one keystroke, including
detecting the beginning and end of the table, the column widths, and
so on.  Not everybody uses their editor that way, but people who make
such suites in tabular form *should* -- time is not an issue here.

*Despite* the above, I don't like the OP's format.  With Andrew
Barnert's suggestion of a dictionary, I get all the above benefits,
with *less* detritus (no "if", "condition==", "return"; jus' the facs,
ma'am) in the tabular format, conformity to common practice
("intuitive" is just an alternative spelling of "familiar"<wink/>),
and it's monkey-patchable if I need to add a new condition at runtime.

 > > And here is some real code that I wrote (not worth explaining in
 > > detail).
 > >
 > >          assert PN[0].isalpha()    ; FirstPart  = PN[0] ; PN = PN[1:].lstrip(Seps) # Must be a letter
 > >          if PN[0].isalpha()        : FirstPart += PN[0] ; PN = PN[1:].lstrip(Seps) # May be a second letter
 > That second line uses a layout that I wish was a SyntaxError,
 > because it is ambiguous whether
 > if cond: statementA; statementB
 > should be grouped as follows (using braces as visual aids):
 > if cond:
 >      { statementA; statementB }
 > or like this:
 > { if cond: statementA }
 > statementB

I don't have a problem with it from this point of view, because the
clear intent is that the semicolons separate the statements of the
suite controlled by the if, and that's what they do.  I do have a
problem with the fact that 'assert' does not introduce a suite.
"Syntax must not look like grit on Tim's screen!"  Besides, it would
screw up my Lisp.<wink/>

More information about the Python-ideas mailing list