Inheriting from the list object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    Inheriting from the list object

    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


    Code:
    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by kdt
    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


    Code:
    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
    [code=Python]class StandardDev(lis t):
    def __init__(self, theList):
    self.theList = theList

    def standardDev(sel f):
    import math
    sums = 0
    n = len(self.theLis t)
    #Calculate the mean average
    ma = (1/float(n))*sum(s elf.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
    >>>
    '''[/code]

    Comment

    • kdt
      New Member
      • Mar 2007
      • 50

      #3
      cheers much again bvdet. your awesome!

      Comment

      • kdt
        New Member
        • Mar 2007
        • 50

        #4
        sorry, last question. How best is it to catch integer division by zero errors on this code?

        Thanks

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by kdt
          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:[code=Python]n = len(self.theLis t)
          if n:
          ....... do stuff......[/code]

          Comment

          • kdt
            New Member
            • Mar 2007
            • 50

            #6
            cheers mate :). I was thinking of using try except, but this works fine.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by bvdet
              It looks like the only way that could happen is when the list is empty. A simple if statement will take care of that:[code=Python]n = len(self.theLis t)
              if n:
              ....... do stuff......[/code]
              Or simply:[code=Python]if self.theList:
              ....... do stuff......[/code]

              Comment

              Working...