Nov 1
Questions about anything so far?
recent new material
Where we are :
- syntax for creating python classes
- the "self" argument in class definitions
- class instance methods (i.e. thing.do(stuff)) and class instance data (i.e. thing.color)
- special class methods such as __init__ and __str__
This week we're practicing using classes and seeing
how they can be used, and introducing a new data structure.
I haven't been talking a lot in class about exactly what's
in the textbook, but I hope you've been reading along -
I think the material we're discussing is covered well there.
homework for this week
How did it go?
Here's my version of part of the homework :
new syntax
__cmp__
A special class method that lets you compare two class instances
with less-than and greater-then.
first_hand = Pair()
second_hand = Pair()
if first_hand > second_hand:
print "First player wins!"
The __cmp__ (compare) method lets us add this behavior to our classes.
First, though we should look at the cmp(a,b) built-in function,
which always returns either -1, 0, or 1 depending on whether
a is more, equal, or less than b. The __cmp__ is method supposed to
do the same thing.
>>> cmp(2,1) # returns 1 since 2 > 1
>>> cmp(2,2) # returns 0 since they're ==
>>> cmp(1,2) # returns -1 since 1 < 2
During class I showed an example how this works :
see the attached cmp_example.py (which was foo.py) .
dictionaries
Python "dictionary" data structure. Like an array, but
with non-numeric "keys". The pairs of things in 'em
are called (key, value) pairs. And there are lots
of tricky methods built in;
jim = { "name" : "Jim", "age": 53 }
print "My name is ", jim['name']
print "My age is ", jim['age']
# Look at dir(jim) to see all the methods like
# jim.keys()
# jim.items()
# jim.has_key(key)
# jim.get(key, value_otherwise)
Dictionaries are very useful data structures. In fact, the symbol table of a running program is essentially just a dictionary, with each variable's name and value. Objects are very like dictionaries, with "slots" for both data and methods. Most of the other CS data structures are things with specific uses built from lists and dictionaries.
Coming: the many uses of dictionaries, including counting things.
aside : method chaining
Sometimes it's useful to have object methods that return
the object itself. That lets you do one thing after another
to the same thing, with a syntax like this :
thing.double().increment().double()
revisiting card.py
With the tricks that we just discussed, can we make a nicer
version of the my Card class?
- use a dictionary rather than a long "if" statement
- use the __cmp__ class to compare cards ... or even pairs of cards ...
Next week's assignment will continue this example,
looking at creating objects that can hold data
and implement behavior of card games.
debugging
At some point we should also talk some more about debugging techniques.
This is something all programmers do, and learning
how is part of the game. Several
of you are getting stuck in the homework, and aren't sure
how to get unstuck. A few ideas :
- Code iteratively, adding only a few pieces before recompiling and retesting. That way when it doesn't work, you know what changed.
- If you're completely stumped, go way back - even to something entirely new - that you know does work, and modify that piece by piece until it's like the thing that isn't working.
- Check all your assumptions. Often the problem isn't quite where you're looking.
- Put in lots of print statements to see if it is doing what you think it should.
- Use the interactive python prompt. Paste in your program piece by piece, and look at everything you can think of.
- Think about how the information should be stored, processed, and which tasks (functions) need what.
- ** Break the problem into small pieces. **
in class
If there's time, I'd like to work on another class programming exercise.
The task is to write a program which will read in a file which has
one word per line, and then to report how many times each word
was seen.
A sample words.txt file is attached.
Here's the start of the program :
"""
count_words.py
Read words.txt which has one word per line.
Count how many times each word appears
and print a summary.
$ python count_words.py
-- count_words --
12 cat
2 dog
2 duck
1 elephant
3 mouse
"""
def main():
print '-- count_words --'
if __name__ == '__main__':
import doctest
doctest.testmod()
main()
And here's the corresponding words.txt file like.
cat
dog
elephant
cat
cat
mouse
mouse
duck
mouse
duck
dog