Python Forum
[NEW CODER] TypeError: Object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[NEW CODER] TypeError: Object is not callable
#6
Your description of the error is confusing. Are you using IDLE?

IDLE is a bad tool. IDLE works differently than other Python IDE's or running Python from the shell. You can never be really sure if odd behaviors are due to an issue is in your code, or if it is another of IDLE's idiosyncrasies.

This is what happens when I run your code in Python. I copied your program to a file named test.py
Output:
(venv) C:\Users...\python_sandbox>python test.py (venv) C:\Users\...\python_sandbox>
Not much interesting there. The file defines a class but doesn't do anything. I add some statements to make it a bit more interesting. The extra staments only execute when this file is executed. They do not execute when this file is imported by another file.
class Staff:
    def __init__(self, pPosition, pName, pPay):
        self.position = pPosition
        self.name = pName
        self.pay = pPay
        print("Creating Staff object")

    def __str__(self):
        return "Position = %s, Name = %s, Pay = %d" % (
            self.position,
            self.name,
            self.pay,
        )

    def calculatePay(self):
        prompt = "\nEnter number of hours worked for %s: " % (self.name)
        hours = input(prompt)
        prompt = "Enter the hourly rate for %s: " % (self.name)
        hourlyRate = input(prompt)
        self.pay = int(hours) * int(hourlyRate)
        return self.pay


if __name__ == "__main__":
    # A little test program5
    officeStaff1 = Staff("Basic", "Michael", 1)
    print(officeStaff1.name)
    print(officeStaff1.position)
    print(officeStaff1.calculatePay())
Output:
(venv) C:\...\python_sandbox>python test.py Creating Staff object Michael Basic Enter number of hours worked for Michael: 5 Enter the hourly rate for Michael: 2 10 (venv) C:\...\python_sandbox>
Another way I can use his code is to start an interactive python interpreter, import the module, and type in python statements.
Output:
(venv) C:\...\python_sandbox>python Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from test import Staff >>> jeff = Staff("Manager", "Jeff", 5) Creating Staff object >>> jeff.name 'Jeff' >>> jeff.position 'Manager' >>> jeff.pay 5 >>> jeff.calculatePay() Enter number of hours worked for Jeff: 4 Enter the hourly rate for Jeff: 5 20 >>> quit()
Notice that Michael was not automatically created when I imported the file. This is because __name__ only equals "__main__" when executing a Python file. When importing a file, __name__ == filename of the imported module.

To make something that works like IDLE, I need to run the python program and use the "-i" option to tell Python to leave the interpreter open after executing the file. Since I am executing the file, I'll remove the test code at the bottom. I am tired of Michael.
Output:
(venv) C:\...\python_sandbox>python -i junk.py >>> jeff = Staff("Manager", "Jeff", 5) Creating Staff object >>> jeff.name 'Jeff' >>> jeff.position 'Manager' >>> jeff.pay 5 >>> jeff.calculatePay() Enter number of hours worked for Jeff: 4 Enter the hourly rate for Jeff: 5 20 >>> quit()
Some comments about your class:

Unless you are writing a class designed for input or output, it is unusual to have the class do input or output. Somebody using your class probably doesn't want it to print "Creating Staff object" each time they create an instance of your class. It is unlikely they will use the calculatePay() method because they probably have hours for the employee in a file or database and don't want to type in the values. I would write your class more like this:
class Staff:
    def __init__(self, position, name, rate):
        self.position = position
        self.name = name
        self.rate = rate

    def __str__(self):
        return f"{self.name}, {self.position}, Rate = {self.rate}"

    def pay(self, hours, rate=None):
        return hours * self.rate if rate is None else rate


if __name__ == "__main__":
    emp = Staff("CEO", "Guido", 42)
    print(f"{emp}. Pay for 10 hours = {emp.pay(10)}")
And now I can use that class in another program.
from staff import Staff  # Renamed generic class module "staff.py"

name, position, rate = input("Enter Employee Name, position, wage: ").split(",")
emp = Staff(position.strip(), name.strip(), float(rate))
hours = float(input(f"Enter hours for {emp.name}: "))
print(f"{emp} pay = {emp.pay(hours)}")
Output:
(venv) C...python_sandbox>python test_staff.py Enter Employee Name, position, wage: Guido, CEO, 42 Enter hours for Guido: 5 Guido, CEO, Rate = 42.0 pay = 210.0
Reply


Messages In This Thread
RE: [NEW CODER] TypeError: Object is not callable - by deanhystad - Aug-23-2023, 06:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 184 May-25-2024, 07:58 PM
Last Post: deanhystad
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 560 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 625 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 852 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,185 Aug-24-2023, 05:14 PM
Last Post: snippsat
  Need help with 'str' object is not callable error. Fare 4 938 Jul-23-2023, 02:25 PM
Last Post: Fare
  TypeError: 'float' object is not callable #1 isdito2001 1 1,152 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,698 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  'SSHClient' object is not callable 3lnyn0 1 1,247 Dec-15-2022, 03:40 AM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,615 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