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)

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. What do I need to do to get it to work?
You should try something like the following outside the class: stats = BasicStats() and then print(stats.findMean(arr))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.