[Tutor] Strange list behaviour in classes

Alan Gauld alan.gauld at btinternet.com
Thu Feb 25 01:33:50 CET 2010


"James Reynolds" <eire1130 at gmail.com> wrote

> I understand, but if self.value is any number other then 0, then the 
> "for"
> will append to the square list, in which case square_list will always 
> have
> some len greater than 0 when "value" is greater than 0?

And if value does equal zero?

Actually I'm confused by value because you treat it as both an
integer and a collection in different places?

> Is this an occasion which is best suited for a try:, except statement? Or
> should it, in general, but checked with "if's". Which is more expensive?

try/except is the Python way :-)

> def variance(self, *value_list):
>    if self.value == 0:
>         var = 0
>    else:
>          average = self.mean(*self.value)
>          for n in range(len(self.value)):
>               square = (self.value[n] - average)**2
>               self.square_list.append(square)
>     var = sum(self.square_list) / len(self.square_list)
>     return var


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list