1

Im very new to programming and am looking for a bit of help improving a piece of code. I've gotten the desired outcome for my tutorial work, but its not done in a great way.

class Person:
 def __init__(self, first_name, last_name, date_of_birth):
 self.first_name = first_name
 self.last_name = last_name
 self.date_of_birth = date_of_birth
 def get_details(self):
 return(f"Name: {self.first_name} {self.last_name}\nDate of Birth: {self.date_of_birth}")
class Worker:
 def__init__(self, tax_file_number, super_number):
 self.tax_file_number = tax_file_number
 self.super_number = super_number
 def get_info(self):
 return(f"TFN: {self.tax_file_number}\nSuper: {self.super_number}")
class Employee(Person, Worker):
 def __init__(self, first_name, last_name, date_of_birth, tax_file_number, super_number, employee_id, position):
 Person.__init__(self, first_name, last_name, date_of_birth)
 Worker.__init__(self, tax_file_number, super_number)
 self.employee_id = employee_id
 self.position = position
 def get_info(self):
 return(f"Employee ID: {self.employee_id}\nPosition: {self.position}")
 
 def get_details(self):
 person = (Person.get_details(self)) + "\n" + (Employee.get_info(self)) + "\n" + (Worker.get_info(self))
 return person
p = Person("Kim", "White", "12/08/2020")
print(p.get_details())
print()
w = Worker(4556655, 567)
print(w.get_info())
print()
e = Employee('Kim', 'White', '12/08/2020', 4556655, 567, 1121, 'Developer')
print(e.get_details())

The issue for me is Employee Class with the multiple inheritance,
Do I really need to initialize all the attributes like that? I thought using inheritance it would take it from the other classes,
but this is how I managed to get it to work.

I was trying to make it so it would be more like,

class Employee(Person, Worker):
 def init(self, employee_id, position):
 self.employee_id = employee_id
 self.position = position
 #*Line of code to make it so the rest of the attributes come in here #
chepner
538k77 gold badges595 silver badges747 bronze badges
asked May 16, 2021 at 1:57
1
  • your current code is a pain to read, can you please format it properly? Commented May 16, 2021 at 2:02

1 Answer 1

1

Using super() in all three classes, you can ensure that each initializer is called in the correct order, and only needs to handles its own new attributes explicitly.

class Person:
 def __init__(self, first_name, last_name, date_of_birth, **kwargs):
 super().__init__(**kwargs)
 self.first_name = first_name
 self.last_name = last_name
 self.date_of_birth = date_of_birth
 def get_details(self):
 return(f"Name: {self.first_name} {self.last_name}\nDate of Birth: {self.date_of_birth}")
class Worker:
 def__init__(self, tax_file_number, super_number, **kwargs):
 super().__init__(**kwargs)
 self.tax_file_number = tax_file_number
 self.super_number = super_number
 def get_info(self):
 return(f"TFN: {self.tax_file_number}\nSuper: {self.super_number}")
class Employee(Person, Worker):
 def __init__(self, employee_id, position, **kwargs):
 super().__init__(**kwargs)
 self.employee_id = employee_id
 self.position = position
 def get_info(self):
 return(f"Employee ID: {self.employee_id}\nPosition: {self.position}")
 
 def get_details(self):
 person = (Person.get_details(self)) + "\n" + (Employee.get_info(self)) + "\n" + (Worker.get_info(self))
 return person
e = Employee(first_name='Kim', last_name='White', date_of_birth='12/08/2020', tax_file_number=4556655, super_number=567, employee_id=1121, position='Developer')

Each __init__ method only needs to name the attributes that it introduces; any other keyword arguments are passed on to a parent class. Eventually, one of the calls to super().__init__ should invoke object.__init__, by which time you should have "consumed" all of the keyword arguments and kwargs should be empty.

For more information about using super correctly to support multiple inheritance, see Python's super() considered super!


You can probably also define get_details() in each class similarly, returning super().get_details + "\n" + [my details here].

answered May 16, 2021 at 2:04
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.