Search code examples
pythonclasscallmultiple-inheritance

Creating a call function for multiple classes


So I got my code to work, but two lines are messing me up, and not quite sure what to put.

Code(the two lines are marked):

class Person(object):
    numPerson = 0
    def __init__(self,firstName,lastName):
        self.firstName = firstName
        self.lastName = lastName
    def fullName(self):
        print self.firstName +' '+self.lastName

class Employee(Person):
    numEmployee = 0
    def __init__(self,firstName,lastName,pay,employID):
        Person.__init__(self, firstName, lastName)
        self.pay = pay
        self.employID = employID
        Employee.numEmployee += 1

class Programmer(Employee):
    def __init__(self,firstName,lastName,pay,employID,proLang):
        self.proLang = proLang
        Employee.__init__(self, firstName, lastName, pay, employID)

class Manager(Employee):
    def __init__(self,firstName,lastName,pay,employID,progList):
        self.progList = progList
        Employee.__init__(self, firstName, lastName, pay, employID)
    def addProgrammer(self):
        self.progList.append(Programmer.fullName) <------------------- This line
    def removeProgrammer(self):
        if len(self.progList) == 0:
            pass
        else:
            del self.progList[0]
    def printList(self):
        print self.progList

a = Manager('Alfred','Jones',20.00,0001,[])
b = Programmer('James','Smith', 11.75, 0002, 'Java')
a.addProgrammer() <--------------------------------------------- And This line
a.printList()

I'm trying to add the programmer's name to the progList using the .addProgramer method. I keep trying different combos and this is the closest I got.

Output:

[<unbound method Programmer.fullName>]

So, I'm not sure what needs to be in the addProgramer method in order to properly add the programmers name, or if I need an argument inside the a.addProgrammer at the very end.


Solution

  • Here:

    self.progList.append(Programmer.fullName)
    

    You're not adding an instance of a programmer, you are adding a method from the programmer class.

    Also:

    def fullName(self):
        print self.firstName +' '+self.lastName
    

    This doesn't actually return the name of the programmer, it only prints it to the console. To actually output and use the the fullname you need to return self.firstName + ' ' + self.lastName

    Likewise in that function you also need to specify which programmer you are adding:

    def addProgrammer(self, added_programmer):
        self.progList.append(added_programmer.fullName()) # Call the function to get the fullname
    

    And now to add a programmer:

    Alfred = Manager('Alfred','Jones',20.00,0001,[]) #make a manager     
    James = Programmer('James','Smith', 11.75, 0002, 'Java') #make a programmer
    Alfred.addProgrammer(James) #add the programmer
    Alfred.printList()
    

    Putting this all together:

    class Person(object):
        numPerson = 0
        def __init__(self,firstName,lastName):
            self.firstName = firstName
            self.lastName = lastName
        def fullName(self):
            return self.firstName +' '+self.lastName
    
    class Employee(Person):
        numEmployee = 0
        def __init__(self,firstName,lastName,pay,employID):
            Person.__init__(self, firstName, lastName)
            self.pay = pay
            self.employID = employID
            Employee.numEmployee += 1
    
    class Programmer(Employee):
        def __init__(self,firstName,lastName,pay,employID,proLang):
            self.proLang = proLang
            Employee.__init__(self, firstName, lastName, pay, employID)
    
    class Manager(Employee):
        def __init__(self,firstName,lastName,pay,employID,progList):
            self.progList = progList
            Employee.__init__(self, firstName, lastName, pay, employID)
        def addProgrammer(self, added_programmer):
            self.progList.append(added_programmer.fullName()) # Call the function to get the fullname
        def removeProgrammer(self):
            if len(self.progList) == 0:
                pass
            else:
                del self.progList[0]
        def printList(self):
            print self.progList
    
    Alfred = Manager('Alfred','Jones',20.00,1,[])       
    James = Programmer('James','Smith', 11.75, 2, 'Java')
    Alfred.addProgrammer(James)
    Alfred.printList()