# [Tutor] Strange list behaviour in classes

James Reynolds eire1130 at gmail.com
Wed Feb 24 04:53:07 CET 2010

```This thread inspired me to start learning object oriented as well, but it
seems I must be missing something fundamental. If I could get
an explanation of why I am raising the following exception, I would
greatly appreciate it.

I'm getting:

Traceback (most recent call last):
File "C:\Python31\Lib\COI\Project\test.py", line 8, in <module>
median = Statistics.stats.median(*a)
File "C:\Python31\Lib\COI\Project\Statistics.py", line 23, in median
n = (self.value[m] + self.value[m+1]) / 2
IndexError: tuple index out of range

Module 1:

import Statistics

a = [1,2,3,4,5,6,7,8,9,10]

mean = Statistics.stats.mean(a)
median = Statistics.stats.median(*a)
stdev = Statistics.stats.stdev(*a)
z = Statistics.stats.zscore(5, *a)
print(mean, median, stdev, z)
print()

Module 2:

#!/usr/bin/python
# Filename: Statistics.py

import math
value_list = []
class Statistics:
def __init__(self, *value_list):
self.value = value_list
#self.average = mean(*value_list)
self.square_list= []
def mean(self, *value_list):
ave = sum(self.value) #/ len(value_list)
return ave

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

stats = Statistics(*value_list)

On Tue, Feb 23, 2010 at 10:27 PM, Lie Ryan <lie.1296 at gmail.com> wrote:

> On 02/24/10 10:27, C M Caine wrote:
> > Thanks all (again). I've read the classes tutorial in its entirety
> > now, the problem I had didn't seem to have been mentioned at any point
> > explicitly. I'm still a fairly inexperienced programmer, however, so
> > maybe I missed something in there or maybe this is a standard
> > procedure in other OO programming languages.
>
> Not exactly, staticcally-typed languages typically uses keywords (like
> "static") to declare an variable as a class variable; but since in
> python, you don't need to do variable declaration the chosen design is
> to define class variable in the class itself and instance variable
> inside __init__() [actually this is not a precise description of what's
> actually happening, but it'll suffice for newbies]
>
> class MyClass(object):
>    classvariable = 'classvar'
>    def __init__(self):
>        self.instancevariable = 'instvar'
>
> if you want to access class attribute from inside a method, you prefix
> the attribute's name with the class' name, and if you want to access
> instance attribute from inside a method, prefix with self:
>
> class MyClass(object):
>    classvariable = 'classvar'
>    def __init__(self):
>        self.instancevariable = 'instvar'
>    def method(self):
>        print MyClass.classvariable
>        print self.instancevariable
>
>
> But due to attribute name resolution rule, you can also access a class
> variable from self:
>
> class MyClass(object):
>    classvariable = 'classvar'
>    def __init__(self):
>        self.instancevariable = 'instvar'
>    def method(self):
>        print self.classvariable
>
>
> as long as the class variable isn't shadowed by an instance variable
>
> class MyClass(object):
>    var = 'classvar'
>    def method(self):
>        print self.var    #'classvar'
>        self.var = 'instvar'
>        print self.var    #'instvar'
>        del self.var
>        print self.var    #'classvar'
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100223/a412b3c1/attachment.html>
```

More information about the Tutor mailing list