Page 234 -
P. 234
no dumb questions
Q: I’m not sure I see why the top3() method is coded to return Q: OK, I think I’m convinced. But tell me: how do I go about
a three-item list, as opposed to a string? Surely a string would adding more times to my existing Athlete objects?
make the print() statement in the main program easier to write?
A: To do more, add more methods. With your Athlete class
A: It would, but it wouldn’t be as flexible. By returning a list (albeit created, it’s a breeze to extend it to do more work for you: simply add
a small one), the top3() method lets the calling code decide what more methods.
happens next, as opposed to forcing the caller to work with a string.
Granted, the current program needs to treat the list like a string, but So, if you want to add a single new timing value to your times
not all programs will want or need to. attribute, define a method called add_time() to do it for you.
Q: Why does the class even need the top3() method? Why Additionally, you can add a list of times by defining a method called
add_times().Then all you need to do in your code is say
not store the top three times as an attribute within the class and something like this:
create it as part of the object’s creation? sarah.add_time('1.31')
to add a single time to Sarah’s timing data, or say this:
A: Again, better not to, because doing so is less flexible. If you james.add_times(['1.21','2.22'])
compute and store the top three times at object creation, you make it to add a bunch of times to James’s data.
harder to extend the list of timing data associated with the object. Q:
But surely, knowing that times is a list, I could write code
For instance, if you add more timing data after the object is created, like this to do the same thing?
you’ll need to arrange to recompute the top three (because the new sarah.times.append('1.31')
times might be fast) and update the attribute. However, when you james.times.append(['1.21','2.22'])
compute the top three times “on the fly” using a call to the top3()
method, you always ensure you’re using the most up-to-date data. A: You could, but that would be a disaster.
Q: OK, I get that. But, with a little extra work, I could do it Q:
during object creation, right? What?!? Why do you say that? There’s nothing wrong
with my suggestion, is there?
A: Well, yes…but we really don’t advise that. By preserving the
original data in each object’s attributes, you are supporting the A: Well…it does indeed work. However, the problem with writing
extension of the class to support additional requirements in the code like that is that it exposes (and exploits) that fact that the
future (whatever they might be). If you process the data as part of timing data is stored in a list within the Athlete class. If you
the object initialization code, the assumptions you make about how later change your class implementation to use (for instance) a string
programmers will use your class might just come back to bite you. instead of a list, you may well break all of the existing code that uses
your class and that exploits the fact that the timing data is a list.
Q: But what if I’m the only programmer that’ll ever use a
custom class that I write? By defining your own API with add_time() and add_
times(), you leave open the possibility that the way the data
is stored within your class can change in the future (obviously, only
A: Trust us: you’ll thank yourself for coding your class to be as if such a change makes sense). It is worth noting that one of the
flexible as possible when you come to use it for some other purpose reasons for using object orientation is to hide away the details of a
in a future project. When you are creating a class, you have no idea class’s implementation from the users of that class. Defining your
how it will be used by other programmers in their projects. And, if you own API directly supports this design ideal. Exposing the internals of
think about, you have no idea how you might use it in the future, too. your class’s implementation and expecting programmers to exploit it
breaks this fundamental ideal in a very big way.
198 Chapter 6