472,119 Members | 969 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Inheriting from the list object

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
6 1331
bvdet
2,851 Expert Mod 2GB
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
kdt
50
cheers much again bvdet. your awesome!
Aug 14 '07 #3
kdt
50
sorry, last question. How best is it to catch integer division by zero errors on this code?

Thanks
Aug 14 '07 #4
bvdet
2,851 Expert Mod 2GB
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
kdt
50
cheers mate :). I was thinking of using try except, but this works fine.
Aug 14 '07 #6
bartonc
6,596 Expert 4TB
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.

Similar topics

15 posts views Thread by JustSomeGuy | last post: by
11 posts views Thread by Noah Coad [MVP .NET/C#] | last post: by
10 posts views Thread by Andrew McLellan | last post: by
2 posts views Thread by YYZ | last post: by
17 posts views Thread by Adrian Hawryluk | last post: by
3 posts views Thread by Mangabasi | last post: by
4 posts views Thread by AalaarDB | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.