return reduce(lambda x, y: x.grade+y.grade, self.reviews)

George Sakkis george.sakkis at gmail.com
Fri Aug 29 14:20:30 EDT 2008


On Aug 29, 1:54 pm, cnb <circularf... at yahoo.se> wrote:
> class Customer(object):
>     def __init__(self, idnumber, review):
>         self.idnumber = idnumber
>         self.reviews = [review]
>
>     def addReview(self, review):
>         self.reviews.append(review)
>
>     def averageGrade(self):
>         tot = 0
>         for review in self.reviews:
>             tot += review.grade
>         return tot / len(self.reviews)
>
>     def av_grade(self):
>         return reduce(lambda x, y: x.grade+y.grade, self.reviews)
>
> now,the function is pointless and incorrect but av_grade doesn't work.
> why can't i use reduce like this?

Because in reduce's lambda, 'x' is the result accumulated so far and
'y' is the next element in the iterable. In this case, after you have
consumed the first two reviews, 'x' is a number (the sum of the first
two grades) and 'y' is the third review, so you're trying to add a
number to a review and you get an error. As a rule of thumb, never use
reduce(); it's rarely useful and can always be written out explicitly
as a loop.

What you want can be expressed much easier and efficiently with a
generator expression as:

     def av_grade(self):
         # XXX: missing 0 reviews handling
         return sum(review.grade for review in self.reviews) /
len(self.reviews)

HTH,
George



More information about the Python-list mailing list