Python Forum

Full Version: Type Error: 'in' object is not callable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import statistics as s

class stats:
   
    def mode(self):
        self.mode = s.mode(self.value)
        return(self.mode)
    def median(self):
        self.median = s.median(self.value)
        return(self.median)
    def mean(self):
        self.mean = s.mean(self.value)
        return(self.mean)
    def __init__(self, value):
        self.value = value

if __name__ == "__main__":
        
        testval = [2,3,4,5,8,4,6,3,4,6,8,9,7,5,3,5,7,4,3,2,2,1,4,6,8,6,8,9,3]
        
        object1 = stats(testval)
        print(type(object1.mode()))
        print(type(object1.median))
        assert (object1.mode()) == 3
        assert (object1.median) == 5
        assert (object1.mean) == 5
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-20-1e939491cea4> in <module> 22 print(type(object1.mode())) 23 print(type(object1.median)) ---> 24 assert (object1.mode()) == 3 25 assert (object1.median) == 5 26 assert (object1.mean) == 5 TypeError: 'int' object is not callable
All of your method names apart from __init__, when called assign the method name to an attribute value.
Example
class stats:
    
    def mode(self): # method name is mode
        self.mode = s.mode(self.value) # mode is now assigned as an attribute value
        return(self.mode)
        ...
        ...
When mode is called the first time it works, but when it is called a second time it doesn't because it is now an int that can't be called.
Within an instance of the class, self.method is the class method. So inside mode, self.mode is the method. When you assign to it, you change mode from a callable method to just an integer.

On line 22 mode is the method. But during the call, on line 6, mode is reassigned to the return value of the statistics call (an integer).

The next time the call is attempted (on line 24), object1.mode is no longer a method.
Thanks guys :)

Do basically if I change the name of the returned method it'll work in the assert statement