Reputation: 11
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 #
Upvotes: 1
Views: 80
Reputation: 531165
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]
.
Upvotes: 1