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
  error in class: TypeError: 'str' object is not callable akbarza 2 456 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 921 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,262 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Wrong type error rowan_bradley 6 1,146 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Need help with 'str' object is not callable error. Fare 4 778 Jul-23-2023, 02:25 PM
Last Post: Fare
  Type Error: Unsupported Operand jhancock 2 1,068 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: 'float' object is not callable #1 isdito2001 1 1,046 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  'SSHClient' object is not callable 3lnyn0 1 1,132 Dec-15-2022, 03:40 AM
Last Post: deanhystad
  declaring object parameters with type JonWayn 2 857 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,375 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov

Forum Jump:

User Panel Messages

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