Python Forum
I am getting a few errors while using classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting a few errors while using classes
#1
I am new to using classes in Python and am having a bit of trouble on my homework assignment.
Here is the assignment. It is kind of long so I put it in a pastebin.
https://pastebin.com/zsMf26nf

I am getting several errors and I am not sure what the problem is. I tried looking on StackOverflow on how to fix it but I didn't find anything very helpful. I don't know what the 0x0000 errors mean. Hopefully, someone can explain it to me because I am very confused about the issue I am having.


Here are the errors I am getting.

Error:
<__main__.Employee object at 0x0000022C8187BFD0> <__main__.ProductionWorker object at 0x0000022C8187BF70> <__main__.Employee object at 0x0000022C8187B970> <__main__.ProductionWorker object at 0x0000022C8187BFD0>
I also was getting this error but I think I was able to fix it. I am not really sure why I was getting it because from reading it there are only 4 arguments and not 5.
Error:
worker_obj = ProductionWorker("Johnson White", "305", "2", "30") TypeError: __init__() takes 4 positional arguments but 5 were given
Johnson White argument 1, 305 argument 2, 2 argument 3, 30 argument 4. Maybe someone can explain this to me because I don't understand it.


I am using Windows and Python 3.9.4.


Here is my code:

class Employee ():
    def __init__(self, name, employee_number):
        self.name = name
        self.employee_number = employee_number


class ProductionWorker(Employee):
    def __init__(self, name, employee_number, shift_number, pay_rate):
        self.name = name
        self.employee_number = employee_number
        self.shift_number = shift_number
        self.pay_rate = pay_rate


# create an employee
emp_obj = Employee("Mary Johnson", "202")
# name: Mary Johnson, employee_number: 202

# create a production worker
worker_obj = ProductionWorker("Johnson White", "305", "2", "30")
# name: Johnson White, employee_number: 305, shift_number: 2, pay_rate: 30


# I am not sure if I am doing the print statements correctly or not
# Print Mary information
print(emp_obj)
# Mary Johnson 202


# Print Johnson Information
print(worker_obj)
# Johnson White 305 2 30

# update Mary Information
emp_obj = Employee("Mary Smith", "202")

# print Mary updated information
print(emp_obj)
# Mary Smith 202

# update Johnson name to Jason and pay to 32
worker_obj = ProductionWorker("Jason White", "305", "2", "32")

print(worker_obj)
# Jason White 305 2 32
Reply
#2
The first set of "errors" are not errors at all, but merely the default string representations of your objects that Python gives you. As you can tell, it's not very meaningful. You can provide your own by overriding the __str__ method.
Reply
#3
(Apr-10-2021, 06:54 PM)alex_0 Wrote: print(emp_obj)
When you do this, the memory address where the object resides is printed. Indeed not very useful.
What you want is something like this:
print(f"Name: {emp_obj.name}\nNumber: {emp_obj.employee_number}")
Output:
Name: Mary Johnson Number: 202
You had best define a method in your class to return the desired information. And as ndc85430 states: a clever way to do this is to redefine the __str__() method.
Reply
#4
You can use dataclasses and get the string output for free
from dataclasses import dataclass

@dataclass
class Employee:
    name: str
    employee_number: str
 
@dataclass
class ProductionWorker(Employee):
    shift_number: str
    pay_rate: str
 
 
# create an employee
emp_obj = Employee("Mary Johnson", "202")
# name: Mary Johnson, employee_number: 202
 
# create a production worker
worker_obj = ProductionWorker("Johnson White", "305", "2", "30")
# name: Johnson White, employee_number: 305, shift_number: 2, pay_rate: 30
 
 
# I am not sure if I am doing the print statements correctly or not
# Print Mary information
print(emp_obj)
# Mary Johnson 202
 
 
# Print Johnson Information
print(worker_obj)
# Johnson White 305 2 30
 
# update Mary Information
emp_obj = Employee("Mary Smith", "202")
 
# print Mary updated information
print(emp_obj)
# Mary Smith 202
 
# update Johnson name to Jason and pay to 32
worker_obj = ProductionWorker("Jason White", "305", "2", "32")
 
print(worker_obj)
# Jason White 305 2 32
Output:
Employee(name='Mary Johnson', employee_number='202') ProductionWorker(name='Johnson White', employee_number='305', shift_number='2', pay_rate='30') Employee(name='Mary Smith', employee_number='202') ProductionWorker(name='Jason White', employee_number='305', shift_number='2', pay_rate='32')
ibreeden, GOTO10, ndc85430 like this post
Reply
#5
(Apr-10-2021, 07:48 PM)ndc85430 Wrote: The first set of "errors" are not errors at all, but merely the default string representations of your objects that Python gives you. As you can tell, it's not very meaningful. You can provide your own by overriding the __str__ method.

Thank you very much for your help.
Reply
#6
(Apr-11-2021, 11:15 AM)Yoriz Wrote: You can use dataclasses and get the string output for free
from dataclasses import dataclass

@dataclass
class Employee:
    name: str
    employee_number: str
 
@dataclass
class ProductionWorker(Employee):
    shift_number: str
    pay_rate: str
 
 
# create an employee
emp_obj = Employee("Mary Johnson", "202")
# name: Mary Johnson, employee_number: 202
 
# create a production worker
worker_obj = ProductionWorker("Johnson White", "305", "2", "30")
# name: Johnson White, employee_number: 305, shift_number: 2, pay_rate: 30
 
 
# I am not sure if I am doing the print statements correctly or not
# Print Mary information
print(emp_obj)
# Mary Johnson 202
 
 
# Print Johnson Information
print(worker_obj)
# Johnson White 305 2 30
 
# update Mary Information
emp_obj = Employee("Mary Smith", "202")
 
# print Mary updated information
print(emp_obj)
# Mary Smith 202
 
# update Johnson name to Jason and pay to 32
worker_obj = ProductionWorker("Jason White", "305", "2", "32")
 
print(worker_obj)
# Jason White 305 2 32
Output:
Employee(name='Mary Johnson', employee_number='202') ProductionWorker(name='Johnson White', employee_number='305', shift_number='2', pay_rate='30') Employee(name='Mary Smith', employee_number='202') ProductionWorker(name='Jason White', employee_number='305', shift_number='2', pay_rate='32')

Thank you very much for your help.
Reply


Forum Jump:

User Panel Messages

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