Software Design Lecture Notes Fall 2004 For Tuesday: 1) work on Homework 7 2) work on your project proposal Survey notes ------------ Thanks for your comments on the surveys. Number of hours seems about right. Some people taking less time, few taking much more. Pace is good for many people. More "too slows" than "too fasts". Some assorted comments: 1) writing code on paper is a pain 2) some people like the book, some like the notes, etc. different things are working for different people 3) more individual feedback would be good Leftovers --------- 1) object invariants 2) inheritance Homework 6 Solutions -------------------- Notice the use of inheritance to define a new kind of Exception: class Missed(Exception): pass Then you can raise a Missed exception: def apply_tag(self, other): # try to tag the other turtle. if it is too far away, # let the other turtle flee and raise an exception; # otherwise return None if self.distance_from(other) < 10: self.not_it() other.youre_it() else: other.flee(self) raise Missed And catch it: try: self.apply_tag(target) except Missed: self.chase(target) Also notice two uses of the DSU pattern: 1) to find the closest turtle t = [(self.distance_from(animal), animal) for animal in others if animal is not self] (distance, animal) = min(t) 2) to find the angle (dt) that would bring the turtle's heading closest to the goal diff = [(diff_dir(self.heading+dt, self.goal), dt) for dt in t] return min(diff)[1] In both cases I uses a list comprehension to build a list of tuples. What was the secret hidden point of this homework? Homework 7 Comments ------------------- getattr and setattr are built-in functions. Try this: p = Point() p.x = 0 getattr(p, 'x') setattr(p, 'y', 100) print p.y What is a data structure? What are the operations we want to support for the homework? What data structures should we consider?