On 06/19/2017 01:32 PM, Evuraan wrote: > Greetings! > > > #!/usr/bin/python3 > class Employee: > """Class with FirstName, LastName, Salary""" > def __init__(self, FirstName,LastName, Salary): > self.FirstName = FirstName > self.LastName = LastName > self.Salary = Salary > def __str__(self): > return '("{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary) > class Developer(Employee): > """Define a subclass, augment with ProgLang""" > def __init__(self, FirstName,LastName, Salary, ProgLang): > Employee.__init__(self, FirstName,LastName, Salary) > self.ProgLang = ProgLang > def dev_repr(self): > return '("{}" "{}" "{}" "{}")'.format(self.FirstName, > self.LastName, self.Salary, self.ProgLang) > a = Employee("Abigail", "Buchard", 83000) > print(a) > dev_1 = Developer("Samson", "Sue", 63000, "Cobol",) > print(dev_1) > print(dev_1.dev_repr()) > > running that yields, > > ("Abigail" "Buchard" "83000") > ("Samson" "Sue" "63000") > ("Samson" "Sue" "63000" "Cobol") > > My doubt is, how can we set the __str__ method work on the Employee > subclass so that it would show ProgLang too, like the > print(dev_1.dev_repr())? Use super() to call up to the base class, and then add the extra bits pertaining to the derived class.
That is, add this to your Developer subclass: def __str__(self): sup = super().__str__() return '{} "{}")'.format(sup, self.ProgLang) You'd need to do a little bit of work to clean up the output, as the string representation returned by the base class is already closed with a paren (see second line of output below): ("Abigail" "Buchard" "83000") ("Samson" "Sue" "63000") "Cobol") ("Samson" "Sue" "63000" "Cobol") but it should show a way to approach this problem, anyway _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor