473,692 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from the list object

50 New Member
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 1460
bvdet
2,851 Recognized Expert Moderator Specialist
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 New Member
cheers much again bvdet. your awesome!
Aug 14 '07 #3
kdt
50 New Member
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 Recognized Expert Moderator Specialist
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 New Member
cheers mate :). I was thinking of using try except, but this works fine.
Aug 14 '07 #6
bartonc
6,596 Recognized Expert Expert
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
1705
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 &elem(*iter);
11
2162
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 methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
10
2227
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. The event in the singleton class always evaluates to null even when I cleary subscribe to it. Does anyone have some sample code of how to do this, or is this not possible.
10
2340
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. No problem with class MyList<T> : List<T> { }
2
980
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 different name, frmTest for example. In the code frmPCGGen, is it possible to know the name of the derived form, ie, frmTest? Matt
1
2899
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 related to an invoice -InvoiceCollection: Inherited from Collectionbase class and holds Invoice objects The InvoiceCollection class is as follows:
17
2435
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 and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
3
9963
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
1696
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 : public virtual base
0
8610
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9090
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8968
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8810
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1961
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.