Why ELIF?

Carl Banks pavlovevidence at gmail.com
Sun Oct 11 19:43:40 EDT 2009


On Oct 11, 4:12 pm, Mensanator <mensana... at aol.com> wrote:
> On Oct 11, 5:05 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
>
>
>
>
>
> > On Oct 11, 7:10 am, Grant Edwards <inva... at invalid.invalid> wrote:
>
> > > On 2009-10-11, metal <metal... at gmail.com> wrote:
>
> > > > I wonder the reason for ELIF. it's not aligned with IF, make code ugly
>
> > > It most certainly is aligned with IF:
>
> > > if cond1:
> > > do this
> > > elif cond2:
> > > do that
> > > else:
> > > do the other
>
> > > The "if" "elif" and "else" are all aligned in all of the code
> > > I've ever seen.
>
> > The condition in the elif clause is two columns to the right of the
> > condition in the if clause.
>
> Why does that matter? Isn't whitespace only
> significant at the start of a line?

I don't think it matters.  I'm explaining what the OP is complaining
about.


> > It's a silly thing to worry about, in fact the slight visual
> > distinctness of it probably helps readability.
>
> It doesn't, but you're right, it's silly to
> worry about.

No it helps me, not much, but a little.  Whether the columns line up
or not is a visual clue that can help spot errors.  For instance,
noticing that condition1 and contition2 line up might help me spot the
error in the following code (second clause should be elif).

if condition1:
    xxx()
if contidion2:
    yyy()
else:
    zzz()

It might never help you, but that doesn't mean it can't help others.
I can only recall once or twice being alerted to this mistake by
column alignment, and definitely can recall several times where I
missed it in spite of the extra visual clue.  All I said is it was a
slight visual clue, not an earth-shattering deal maker.


> > Some people do get
> > finicky about columns and try to line things up all the time.
>
> But you can do it if you really want to:
>
> a          =  1
> if      a  >  5:
>   print a
> elif    a  > 10:
>   print a  /  3
> else:
>   print 'error'

Ugh.


> >It's
> > frustrating, wasteful, and ultimately hopeless, and sometimes
> > deceptive (lining things up can suggest relationships where none
> > exists) so I make it a point not to do it, however prettier it'll make
> > those two lines.
>
> The above example is of dubious value. Where I
> use it is places like
>
> ONE   = gmpy.mpz( 1)
> TWO   = gmpy.mpz( 2)
> THREE = gmpy.mpz( 3)
> TEN   = gmpy.mpz(10)

I never line up columns except when defining some kind of table.
(E.g., PyMemberDef in a C module.)  What happens when you have to add
a constant like this:

A_HUNDRED_MILLION = gmmp.mpz(100000000)

Now you have to widen a dozen pair of parentheses, and the readability
suffers when you have all kinds of space separating things:

ONE               = gmpy.mpz(        1)

Plus this has the "implied fictional realtionship" problem I mentioned
(although not too dangerous or misleading here).  It'd have been
better not to have bothered.


Carl Banks



More information about the Python-list mailing list