473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1453
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

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

Similar topics

15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
10
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
10
by: Andrew McLellan | last post by:
I think I must be missing something about generics, perhaps just about the syntax. I'd like to derive a class MyList from System.Collections.Generic so that it can only contain instance of MyItem....
2
by: YYZ | last post by:
I've got a form in a class library, form named frmPCGGen. I'm inheriting from this form (for some base functionality) in a different project. When I inherit from the form, I obviously give it a...
1
by: Kyle Novak | last post by:
I have a question about strongly typed objects when looping through a collection based on the CollectionBase object and using a For..Each loop. I have 2 objects: -Invoice: Holds all properties...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
3
by: Mangabasi | last post by:
Howdy, I would like to create a Point class that lets me use Point instances like the following example. 3 4 1 3 4
4
by: AalaarDB | last post by:
struct base { int x, y, z; base() {x = 0; y = 0; z = 0;}; base(int x1, int y1, int z1) {x = x1; y = y1; z = z1;}; }; struct intermediate1 : public virtual base {}; struct intermediate2 :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.