Python for non-programmers

Kragen Sitaker kragen at dnaco.net
Mon Mar 6 13:25:59 EST 2000


In article <NDBBKEGCHLLCCFMJKMIHEEKPDAAA.infonuovo at email.com>,
Dennis E. Hamilton <infonuovo at email.com> wrote:
>I am teaching my 35-year-old son about programming using Python.

Terrific article.

You might try to convince your son to call himself a "whitesmith" or a
"codesmith" rather than a "cybersmith".  "Cyber-" is one of those
prefixes that marks you as being one of the, um, out-group.

About Python for beginners: I'm not a beginner at programming (at
least, I don't think so; I've never written a program that was more
than 2000 lines long, though), but I'm a beginner at Python.  I had
some irritating problems with error messages when writing my first
useful Python programs this morning.

EOF SyntaxError
---------------

	The more amusing one was the SyntaxError at the end of my
	file.  The interpreter error message helpfully pointed out the
	erroneous line: the blank line at the end of the file, with a
	caret helpfully pointing out the beginning of the line.

	I looked for unterminated strings, mismatched parens, etc., to
	no avail.

	I deleted the several hundred lines of '''-ed out Perl code
	before the end of file.

	SyntaxError.  Blank line with a caret.

	I commented out the last routine in the file.  No Syntax
	Error.

	I uncommented the first line of the routine ("def test1():").
	SyntaxError.  Hmm, guess that's where the problem is.

	At this point, the Alert Python Reader recognizes that I have
	just commented out the original error and inserted a new one.
	The Slow Python Newbie, however, doesn't have any idea.  I had
	a suspicion that was what I had done; I added a 'pass'
	statement, and the SyntaxError went away.

	Line by line, I uncommented the routine, recompiling ("python
	-c 'import rat_cont'") every time.  The error was on (surprise)
	the last line.  I was missing a right-paren.

	The point of this painful, boring story is that the error
	message was extremely unhelpful, almost to the point of being
	actively deceptive.  A little more detail ("expected ) matching
	( on line 102" --- or even "expected )") would have been
	*incredibly* useful.  I spent most of my time looking for
	mismatched string quotes.

	(I hit % in vi a lot more after that, too. :)

	I was going to say that Perl was much better about things like
	this, because it usually diagnoses my syntax errors correctly,
	but in this particular case, it is completely unhelpful.

TypeError
---------

	My second minor irritation was the TypeError.  It explained
	helpfully, "illegal argument type for built-in operation", and
	told me what line it was on.  Unfortunately, that line
	contained several expressions, one of which contained several
	variables and several built-in operations, and as far as I
	could tell, there was no way that any of them could have been
	anything but an integer.

	This really burns me up.  

	The interpreter is executing my program.  It has figured out
	what operation it's executing and what types its operands need
	to be.  It has figured out what subexpressions it's supposed to
	be using the results from, what values it's getting from them,
	what the types of those values are, and that one of those types
	is wrong.

	In order to fix it, I need to know all of these things.  

	So what does the interpeter do?  It throws all of this
	information away except for the last bit and the line number:
	somewhere on line XXX, a built-in operation got a value of
	inappropriate type.  

	I guess it thinks I will enjoy figuring all that stuff out by
	myself; it doesn't want to spoil the fun for me.

	(Don't get me wrong; this is way better than C.  It's just that
	I'm sick and tired of computers, I guess.)

	Fortunately, it was a small program, and rerunning the code
	with a print statement inserted is not a big deal.

	This is one case where Perl is worse, I think; it turned out I
	was trying to multiply something by [2] instead of 2.  That's
	not an error in Perl; try it sometime.  :)

Aside from the jargon-laden error message (try imagining what "illegal
argument type for built-in operation" means to someone for whom
"illegal" means you're going to jail, "argument" is something that
happens when you disagree with someone, "type" in the context of
computers is what you do with the keyboard, and "operation" is what a
person does with a computer.) these error messages are just not very
helpful.  I had a pretty good idea of what kinds of things they could
mean, but I still wasted precious time.
-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>
The power didn't go out on 2000-01-01 either.  :)



More information about the Python-list mailing list