Page 286 -
P. 286
small fix, big difference
A small change can make all the difference
The CGI tracking output indicates an error with the use of the top3() method
from the AthleteList code.
A quick review of the code to the AthleteList class uncovers the source of the
error: the top3() method has been redesignated as a class property.
This decorator allows you to access the data
@property returned by “top3()” as if it were a class
attribute.
def top3(self):
return(sorted(set([self.sanitize(t) for t in self]))[0:3])
The use of the @property decorator allows the top3() method to appear
like an attribute to users of the class. So, instead of calling the top3() A method call
method like this: always needs the
parentheses…
print(yate.u_list(athletes[athlete_name].top3()))
Treat the top3() method as if it was another class attribute, and call it like
this:
print(yate.u_list(athletes[athlete_name].top3))
…unless the method is declared
It’s a small change, but it’s an important one to be an “@property”, in which
When a change is made to the way a class is used, you need to be careful to case parentheses are NOT
consider what impact the change has on existing programs, both yours and required.
those written by others.
At the moment, you are the only one using the AthleteList class, so it’s
not a big deal to fix this. But imagine if thousands of programmers were
using and relying on your code…
Let’s fix your CGI script and try again.
250 Chapter 7