[Tutor] Should this be a list comprehension or something?
Alan Gauld
alan.gauld at freenet.co.uk
Wed Jan 26 23:27:12 CET 2005
Personally no I don;t think it should be a comprehension.
LCs are for building lists, although they sometimes are
abused for other things, but you do not appear to be
building a list per se. The for loop is clearer IMHO.
> Here's my code:
> --------------------
> class Water:
> def __init__(self, WaterMass, WaterTemperature):
> self.mass = WaterMass
> self.temperature = WaterTemperature
> def __repr__(self):
> return ("%.2f, %.2f" % (self.mass, self.temperature))
>
> def CombineWater(WaterList):
> totalmass=0
> numerator = 0; denominator = 0
> for WaterObject in WaterList:
> totalmass += WaterObject.mass
> numerator += WaterObject.mass * WaterObject.temperature
> return Water(totalmass, numerator/totalmass)
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list