[Tutor] taking Python to next level

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Jul 18 09:44:16 CEST 2004



> >> (And it's not necessarily a bad thing to read "bad" code, either: we
> >> learn more quickly from mistakes than from successes.)
> >
> > I'm not so sure about that.
> >
> > First of all, for a beginner who reads someone elses code, it's not so
> > easy to know whether something is bad or good.

Hi Magnus,


Very true; reading code without some accurate sense of the goodness or
badness of it isn't helpful at all; it might even hurt.  I was assuming
that any such code wouldn't be read alone, but would be rigorously
critiqued in a supervised forum like Python-Tutor.  *grin*


> > Secondly, there are an infinite number of ways to write a program.
> > Learning one way *not* to write doesn't seem very helpful.

There may be infinite surface variation, but I think that the common
mistakes can be broken down into a few classes, like overusing globals, or
writing really long functions.  That is, I think there's a common set of
"smells" that I think are indicators of code that needs a rewrite.

    http://c2.com/cgi/wiki?CodeSmell


For example, code like this:

###
"""Reads out a number's digits."""
number = raw_input("type a number: ")
for digit in phone_number:
    if digit == '0':
        print "zero"
    if digit == '1':
        print "one"
    if digit == '2':
        print "two"
    if digit == '3':
        print "three"
    if digit == '4':
        print "four"
    if digit == '5':
        print "five"
    if digit == '6':
        print "six"
    if digit == '7':
        print "seven"
    if digit == '8':
        print "eight"
    if digit == '9':
        print "nine"
###

is functional, but it's also, frankly, ugly, because it's much too long,
and there's so much repetition in there.  Beginners might not have the
experience to know how to fix this, but they should at least try to
develop a sense of the "smell" of a code snippet, so that they can ask
others about how to improve it.


I think it's worthwhile to show nonoptimal code, as long as we also show
why it needs work, and how to fix it.  The carrot and the stick approach,
I guess.  *grin*

I'd better follow my own advice.  Here's one way to rewrite that snippet
above:

###
"""Reads out a number's digits."""
number = raw_input("type a number: ")
lookup_table = ['zero', 'one', 'two', 'three', 'four',
                'five', 'six', 'seven', 'eight', 'nine']
for digit in phone_number:
    print lookup_table[int(digit)]
###



> Any recommendations for projects which provide good models? (I get that
> it would be best to read code in an application area that interests me,
> but I also think my request to be harder to accommodate if I pile on
> conditions ;-)

Orbitz mentioned the Quotient project, which I'm not familiar with at all.
It sounds interesting, though!

    http://www.divmod.org/Home/Projects/Quotient/

The 'Twisted' network framework itself is getting some really high praise
from folks here; it might be interesting to look into that.  I dunno; do
you have a particular application area that interests you?


(Personally, I'm planning to look into Chandler:

    http://www.osafoundation.org/

but that's partially because I'm always losing track of information; a PIM
would probably help me.  *grin*)



More information about the Tutor mailing list