Python Forum
Type Error: 'in' object is not callable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Type Error: 'in' object is not callable (/thread-33511.html)



Type Error: 'in' object is not callable - nman52 - May-01-2021

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



RE: Type Error: 'in' object is not callable - Yoriz - May-01-2021

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.


RE: Type Error: 'in' object is not callable - bowlofred - May-01-2021

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.


RE: Type Error: 'in' object is not callable - nman52 - May-01-2021

Thanks guys :)

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