Python Forum
Type Error: 'in' object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type Error: 'in' object is not callable
#1
Star 
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
Reply
#2
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.
Reply
#3
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.
Reply
#4
Thanks guys :)

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  'module' object is not callable Racer_x 5 277 Nov-04-2021, 03:20 PM
Last Post: Racer_x
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 1,221 Sep-02-2021, 03:24 PM
Last Post: buran
  'str' object is not callable ZYSIA 8 1,039 Aug-03-2021, 03:26 PM
Last Post: deanhystad
  Incorrect Type Error milkycow 4 820 Jun-25-2021, 06:04 AM
Last Post: milkycow
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 998 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Type Error in Python MarcusB 3 827 Mar-30-2021, 06:34 PM
Last Post: buran
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 1,192 Feb-09-2021, 05:40 PM
Last Post: soft
  'int' object is not callable hcccs 5 1,669 Feb-04-2021, 09:15 PM
Last Post: Gribouillis
  'list' object not callable sidra 5 1,932 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  TypeError: 'type' object is not subscriptable Stef 1 1,811 Aug-28-2020, 03:01 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020