I have been trying out some stuff using inheritance with classes in Python 3.4. In the code below, I use the super()
call to return the __str__
from the parent class. Is it correct coding practice to save the information returned by the parent class method in a variable, concatenate the __str__
from the child class to that of the parent class, and return like I did twice below?
Also, what would be best coding practice to convert such output to a string? Would it be better to
- Do this also in the parent class.
- Also in the child class.
- When printing - in this case -
Director
orManager
?
class Employee:
def __init__(self, name, salary, position):
self._name = name
self._salary = salary
self._position = position
def __str__(self):
return str(self._name) + ' ' + str(self._salary) + ' ' + str(self._position)
class Director(Employee):
def __init__(self, name, salary, position, mngteam):
super().__init__(name, salary, position)
self.managementteam = mngteam
def __str__(self):
st = super().__str__() # IS THIS GOOD CODING PRACTICE?
st += ' ' + str(self.managementteam)
return st
class Manager(Employee):
def __init__(self, name, salary, position, supervises):
super().__init__(name, salary, position)
self.supervises = supervises
def __str__(self):
st = super().__str__() # IS THIS GOOD CODING PRACTICE?
st += ' ' + str(self.supervises)
return st
d = Director('DirMax', 100000, 'Director', 5)
print(d)
m = Manager('ManSam', 50000, 'Manager', 10)
print(m)