On 26 Jan 2005, [EMAIL PROTECTED] wrote: > The following Python code works correctly; but I can't help but wonder if > my for loop is better implemented as something else: a list comprehension > or something else more Pythonic.
[Code] > > -------------------- > w1 = Water(50,0) > w2 = Water(50,100) > w3 = Water(25,50) > > print CombineWater((w1,w2,w3)) > -------------------- > > prints, as expected: 125.00, 50.00 What about overloading »+«? class Water(object): def __init__(self, mass, temp): self.mass = mass self.temp = temp def __repr__(self): return ("%.2f, %.2f" % (self.mass, self.temp)) def __add__(self, other): m = self.mass + other.mass return Water(m, (self.mass*self.temp + other.mass*other.temp)/m) .>>> w1 = Water(50,0) .>>> w2 = Water(50, 100) .>>> w3 = Water(25,50) .>>> w1 + w2 + w3 .125.00, 50.00 Karl -- Please do *not* send copies of replies to me. I read the list _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor