464,275 Members | 1,149 Online Need help? Post your question and get tips & solutions from a community of 464,275 IT Pros & Developers. It's quick & easy.

# Inheriting from the list object

 P: 50 Hi I have a function for calculating the standard deviation of a list. I was wanting to create this as an object and inherit from pythons list object. I'm pretty new to the whole OOP concept, can someone please have a look at my code to see why it prints an address rather than a value and where I'm messing this OOP thing up. Thanks Expand|Select|Wrap|Line Numbers class StandardDev(list):     def __init__(self, theList):         self.theList = theList       def standardDev(self):         import math         sums = 0         n = len(self)         #Calculate the mean average         ma = (1/float(n))*sum(self)         for i, j in enumerate(self):             sums += ((self[i]-ma)**2)         return round(math.sqrt((1/float(n))*(sums)),2)     a = (5,6,8,9)   b = StandardDev(a) print b.standardDev     Aug 14 '07 #1
6 Replies

 Expert Mod 2.5K+ P: 2,851 Hi I have a function for calculating the standard deviation of a list. I was wanting to create this as an object and inherit from pythons list object. I'm pretty new to the whole OOP concept, can someone please have a look at my code to see why it prints an address rather than a value and where I'm messing this OOP thing up. Thanks Expand|Select|Wrap|Line Numbers class StandardDev(list):     def __init__(self, theList):         self.theList = theList       def standardDev(self):         import math         sums = 0         n = len(self)         #Calculate the mean average         ma = (1/float(n))*sum(self)         for i, j in enumerate(self):             sums += ((self[i]-ma)**2)         return round(math.sqrt((1/float(n))*(sums)),2)     a = (5,6,8,9)   b = StandardDev(a) print b.standardDev     Expand|Select|Wrap|Line Numbers class StandardDev(list):     def __init__(self, theList):         self.theList = theList       def standardDev(self):         import math         sums = 0         n = len(self.theList)         #Calculate the mean average         ma = (1/float(n))*sum(self.theList)         for i, j in enumerate(self.theList):             sums += ((self.theList[i]-ma)**2)         return round(math.sqrt((1/float(n))*(sums)),2)     a = (5,6,8,9)   b = StandardDev(a) print b.standardDev()   ''' >>> 1.58 >>> ''' Aug 14 '07 #2

 P: 50 cheers much again bvdet. your awesome! Aug 14 '07 #3

 P: 50 sorry, last question. How best is it to catch integer division by zero errors on this code? Thanks Aug 14 '07 #4

 Expert Mod 2.5K+ P: 2,851 sorry, last question. How best is it to catch integer division by zero errors on this code? Thanks It looks like the only way that could happen is when the list is empty. A simple if statement will take care of that: Expand|Select|Wrap|Line Numbers n = len(self.theList) if n:     ....... do stuff...... Aug 14 '07 #5

 P: 50 cheers mate :). I was thinking of using try except, but this works fine. Aug 14 '07 #6

 Expert 5K+ P: 6,596 It looks like the only way that could happen is when the list is empty. A simple if statement will take care of that: Expand|Select|Wrap|Line Numbers n = len(self.theList) if n:     ....... do stuff...... Or simply: Expand|Select|Wrap|Line Numbers if self.theList:     ....... do stuff...... Aug 14 '07 #7 