> 3) No modularization. Couldn't see a reason to do so. Is there one or
> two? Specifically, what sections should become modules, if any?


Hi Dick,

My recommendation is to practice using and writing functions.  For
example, this block here is a good candidate to box off as a named
function:

#######
F = []
for word in L:
    k = L.count(word)
    if (k,word) not in F:
        F.append((k,word))
# F is a list of duples (k, word), where k is the frequency of word
F.sort()
#######


That block can be treated as a function that takes in a list of words, and
returns a list of duples.

######
def doFrequencyCalculation(L):
    # F is a list of duples (k, word), where k is the frequency of word
    F = []
    for word in L:
        k = L.count(word)
        if (k,word) not in F:
            F.append((k,word))
    F.sort()
    return F
######

Here, we explicitly say that frequence calculation depends on having a
list L of words, and that the result should be a list of duples.

Functions allow us to "scope" variables into different kinds of roles.
We'd say in doFrequenceCalculation that 'word' and 'k' are "temporary"
variables.  If we write programs using functions, we can express that
"temporary"  role as a real part of the program.

As it stands, in the original code, we have to watch that 'word' or 'k'
isn't being used by anyone else: like all the other variables in your
program, they're all global and given equal status.  You're using
lowercase as an informal way to remind yourself that those variables are
meant to be temporary, but that approach can be error prone.


Conceptually, your program appears to work in four stages:

     1.  Read lines of a file
     2.  Extract list of words from the lines
     3.  Do histogram frequency count on those lines
     4.  Report frequency results

and it would be great if we could see this program flow as an explicit
sequence of function calls:

##################################
## Pseudocode
def program():
    L = readLinesInFile()
    W = extractWordsFromLines(L)
    F = doFrequencyCalculation(W)
    reportFrequencyResults(F)
##################################


This allows one to see the overall program plan without having to read the
whole program.  It also allows you to do some experimentation later on by
letting you plugging in different functions in the overall program() plan.

As a concrete example: once we have a program in this form, it's easy to
swap in a different algorithm for doing frequency calculation:

######
def program():
    L = readLinesInFile()
    W = extractWordsFromLines(L)
    ## F = doFrequencyCalculation(W)
    F = doFrequencyCalculationWithDifferentAlgorithm(W)
    reportFrequencyResults(F)
######


Does this make sense?  Please feel free to ask more questions about this.

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to