By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 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
  1. class StandardDev(list):
  2.     def __init__(self, theList):
  3.         self.theList = theList
  4.  
  5.     def standardDev(self):
  6.         import math
  7.         sums = 0
  8.         n = len(self)
  9.         #Calculate the mean average
  10.         ma = (1/float(n))*sum(self)
  11.         for i, j in enumerate(self):
  12.             sums += ((self[i]-ma)**2)
  13.         return round(math.sqrt((1/float(n))*(sums)),2)
  14.  
  15.  
  16. a = (5,6,8,9)
  17.  
  18. b = StandardDev(a)
  19. print b.standardDev
  20.  
  21.  
Aug 14 '07 #1
Share this Question
Share on Google+
6 Replies


bvdet
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
  1. class StandardDev(list):
  2.     def __init__(self, theList):
  3.         self.theList = theList
  4.  
  5.     def standardDev(self):
  6.         import math
  7.         sums = 0
  8.         n = len(self)
  9.         #Calculate the mean average
  10.         ma = (1/float(n))*sum(self)
  11.         for i, j in enumerate(self):
  12.             sums += ((self[i]-ma)**2)
  13.         return round(math.sqrt((1/float(n))*(sums)),2)
  14.  
  15.  
  16. a = (5,6,8,9)
  17.  
  18. b = StandardDev(a)
  19. print b.standardDev
  20.  
  21.  
Expand|Select|Wrap|Line Numbers
  1. class StandardDev(list):
  2.     def __init__(self, theList):
  3.         self.theList = theList
  4.  
  5.     def standardDev(self):
  6.         import math
  7.         sums = 0
  8.         n = len(self.theList)
  9.         #Calculate the mean average
  10.         ma = (1/float(n))*sum(self.theList)
  11.         for i, j in enumerate(self.theList):
  12.             sums += ((self.theList[i]-ma)**2)
  13.         return round(math.sqrt((1/float(n))*(sums)),2)
  14.  
  15.  
  16. a = (5,6,8,9)
  17.  
  18. b = StandardDev(a)
  19. print b.standardDev()
  20.  
  21. '''
  22. >>> 1.58
  23. >>>
  24. '''
Aug 14 '07 #2

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

P: 50
kdt
sorry, last question. How best is it to catch integer division by zero errors on this code?

Thanks
Aug 14 '07 #4

bvdet
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
  1. n = len(self.theList)
  2. if n:
  3.     ....... do stuff......
Aug 14 '07 #5

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

bartonc
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
  1. n = len(self.theList)
  2. if n:
  3.     ....... do stuff......
Or simply:
Expand|Select|Wrap|Line Numbers
  1. if self.theList:
  2.     ....... do stuff......
Aug 14 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.