horizontal scrolling (was Re: Larry Wall's comment on python...)

Terry Hancock hancock at anansispaceworks.com
Sun Sep 8 13:27:28 EDT 2002


Hi all,

I wanted to point out a good reason why Python often
led me to overly-wide source files before I learned
more about it. I suspect this is one thing that trips
up a lot of newbies:

In C or Perl (any non-whitespace significant language),
you can easily code long if conditions (or other 
expressions), like this:

if ( condition_one_which_has_a_nice_long_expression ||
   ((condition_two_which_also_is_pretty_long)       &&
    (condition_three_wich_needs_two_to_apply))      ||
     condition_four_also_pretty_darn_long)
	{
	/* Do what I need to do in this oddball case	*/
	}

But the Python intro books usually don't put much emphasis
on how to do this, so you wind up thinking you have to write:

if condition_one_which_has_a_nice_long_expression or
(condition_two_which_also_is_pretty_long and
condition_three_wich_needs_two_to_apply) or
condition_four_also_pretty_darn_long:
	# Do what I need to do in this oddball case
	pass

which is indeed pretty obnoxious. The newbie has been taught
that you don't need () on the if-clause (they may think they
will cause a syntax error  -- which they *do* in csh/tcsh,
which they may have previous experience in, BTW).  So they
take a long time before they get around to trying:

if ( condition_one_which_has_a_nice_long_expression or
     ( condition_two_which_also_is_pretty_long      and
       condition_three_wich_needs_two_to_apply)     or
     condition_four_also_pretty_darn_long ):
	# Do what I need to do in this oddball case
	pass

which is a fairly simple trick that results from the Python
rules for parsing expressions, but isn't usually mentioned
when introducing the 'if' statement.   Maybe it ought to
be obvious, but some of us don't make the connection right
away. ;-D

BTW, I think the wording used at one point was more like
Python will continue any expression it knows to be unfinished,
rather than that is contained in delimiters.  This led me
to wonder why I can't just type:

if condition_one_which_has_a_nice_long_expression  or
   ( condition_two_which_also_is_pretty_long        and
     condition_three_wich_needs_two_to_apply)       or
   condition_four_also_pretty_darn_long:
	# Do what I need to do in this oddball case
	pass

which would seem to be unambiguous, but which causes a
traceback on the first line.  It seems to me that the
dangling operator is enough of a clue that more expression
is to follow. (I think there was a thread about this
a month or two ago, so I won't pursue it -- at least until
after I've reviewed it ;-D).

Anyway, I actually had a Perl instructor make a big deal
about being able to spread an if statement over several
lines like this in Perl, as if it were a big win over
Python.  I didn't have the heart to tell him that it
could be done exactly the same way in Python.

From: Peter Hansen <peter at engcorp.com>
> My point is that indentation (the way I've done it anyway) looks
> *exactly* the same in C, Pascal, Python, or any other block-structured
> language, so I still have no idea why Python would feel any different
> in this respect for someone.
> 
> Clearly I'm missing some point, but I still don't know what it is.
> [... examples ...]
>
> The only thing that would make me exceed 80 columns with a language
> like this is if I couldn't define subroutines and call them, to
> keep the nesting from growing too large.  Since all these languages
> allow that, I have no idea what is special about Python that leads
> to excessive indentation.

You can avoid this problem by always using a function
call to evaluate the expression, but IMHO it often
is bad style (fails the "explicit is better than implicit"
test).

This probably depends on the application domain,
I suspect -- in some types of projects, the expression
will almost always have some kind of logical meaning
in itself, in which case a function makes sense -- but
in other places, it's just a lump of conditions with
only one obvious place to use it.  You could define a
function for one use, but it just requires more typing
without really making anything clearer, IMHO.

BTW -- I always used to write for 132-columns because
that's what my printer printed (way back when).
Nowadays, I hardly ever use a terminal or printer
that is narrower than this.  I find wide formatted
code easier to comprehend (on the whole I don't like
splitting expressions over multiple lines if I can
help it, even though it's sometimes necessary, as I
was trying to point out here). I suppose this is
probably some kind of open-source sin.  Oh well.

Cheers,
Terry


-- 
------------------------------------------------------
Terry Hancock
hancock at anansispaceworks.com       
Anansi Spaceworks                 
http://www.anansispaceworks.com 
P.O. Box 60583                     
Pasadena, CA 91116-6583
------------------------------------------------------




More information about the Python-list mailing list