[Tutor] __str__ on a subclass

Evuraan evuraan at gmail.com
Mon Jun 19 15:32:37 EDT 2017


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())?

Thanks in advance!


More information about the Tutor mailing list