1

When I run the following code I get an AttributeError: 'set' object has no attribute findMean. What am I doing wrong?

class BasicStats:    

    def findMean(self, num = {}):
        length = len(num)

        sum = 0
        for x in num:
            sum = sum + x

        mean =sum/length
        return mean     

    def findVariance(self, num = {}):
        mean = self.findMean(num)
        length = len(num)

        squared_difference = 0
        for x in num:
            squared_difference = squared_difference + (x-mean)**2

        variance = squared_difference/length
        return variance

    arr = {1, 23, 343.34, 2}    
    findVariance(arr)
user2465510
  • 99
  • 10

1 Answers1

3

It's because self in that scope is a set. More specifically, it is arr (which is a set and you pass in as the first argument).

The self keyword only works for functions called against an instance of a class (this special type of functions are called methods, read more here.)

Community
  • 1
  • 1
David Gomes
  • 5,644
  • 16
  • 60
  • 103