Programming Habits in Python

Andrew Dalke dalke at acm.org
Sun Dec 10 14:40:31 EST 2000


A.M. Kuchling wrote:
>I've started reading Knuth's TAoCP several times but never
>gotten very far, mostly because vol. 1 quickly embroils you
>in MIX, making it very hard to distill anything of practical
>value from the book.

I'll second that.  I tried for figure out one of the MIX
examples and found it to be quite difficult.  The examples
do not use structured programming, and the code I tried
could not easily be turned into a set of nested loops.
Instead, the loops were interweaved using gotos.

The problem is that for the last 10 years, my thoughts have
been tuned as it were to hierarchical loops and not arbitrary
tangles, so it was really hard to figure out the example.

I compare that to Kernigan and Plauger's "The Elements of
Programming Style" where even their examples of well written
code uses gotos for the loop constructs.  It's easier to
understand in that case because I could mentally change them
to while loops, but there's still the mental overhead of
having to verify that the jump destinations are correct.

As yet another comparison, I've been doing a lot of work with
mxTextTools, which uses a state table to do text processing.
It's very fast so I rewrote some of my Python parsing code
using it.  While there were only about 12 states, it took
a few hours to write and debug while the Python implementation
took about 15 minutes.

I ended up coding the example in C++ instead of Python because
C++ has gotos and because C++'s STL includes a priority queue.
The whole code ended up being about 5 times longer than the
MIX example, which isn't bad since MIX puts a lot of the
code on one line, and he didn't declare a priority queue.

It would be very hard to rewrite the code for Python because
there are no gotos, so almost by definition that means it's
difficult to "distill anything of practical value from the
book." :)  Theoretical value, yes.  Practical value, no.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list