Nov 1
aside
A couple more interesting python resources :
apologies
I'm (still) not finished grading the midterm projects. They will be done "real soon", hopefully by Fri afternoon.
old homework
Discuss the homework, looking at several of your submitted pieces.
Volunteers welcome.
We went over a "method chaining"
and described how to write objects
that allowed syntax like
thing.double().increment().double)
new business
Continue discussion of classes.
Some examples, including the notion of "inheritance" and "class variables" are in the
code/objects directory .
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.
It includes use of one more special class method, __cmp__ .
cmp(x,y) is a built-in Python function that compares things.
And there are many more "special method names" in classes,
all with names like __*__ , for adding, comparing, and
doing things like making classes behave like functions or
collections (i.e. dictionaries and lists). Thus you can
invent your own, new python data structures, and add 'em in.
new homework
... has been posted.
I want you to use a "template" top down approach to
working on your code : first write the form of the
class and methods, with "fake" return values (no "print" allowed
for this assignment within classes), along with the main()
method that uses the classes. The point is to make sure
that you know what methods and classes you want *before*
you try to implement the specific methods.
other business
We should talk some more about debugging techniques.
This is something all programmars 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.