Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cant figure out this Python behaviour

Options
  • 04-05-2015 2:36pm
    #1
    Registered Users Posts: 1,112 ✭✭✭


    Am currently doing a course in College that includes a Python comprehension test.
    Am not the best at Python, but getting through it for the most part.
    Have come across a sample question though, that I can't for the life of me figure out why it is failing and was hoping someone could point out the obvious fault that I am just not seeing.
    Here is the code
    #!/usr/bin/python
    class Employee:
            def __init__(self, emp_name, emp_dependents=[], emp_salary=0):
                    self.name = emp_name
                    self.dependents = emp_dependents
                    self.salary = emp_salary
    
            def addDependent(self, emp_name):
            # Of course from time to time one adds dependents.
                    self.dependents.append(emp_name)
    
            def dependentNames(self):
                    return str(self.dependents)
    
            def printMe(self):
                    print ("Name:\t%s" % self.name)
                    print ("Dependents:\t%s" % self.dependentNames())
    
    
    And here is the test that is failing.
    from Employee import Employee
    
    import unittest
    
    class TestEmployeeFunctions(unittest.TestCase):
    
            def setUp(self):
                    self.emp1 = Employee("Dylan Thomas", ["Caitlin MacNamara", "Llewelyn Edouard Thomas"])
                    self.emp2 = Employee("Frank O Hara")
                    self.emp3 = Employee("Elizabeth Bishop")
    
            def tearDown(self):
                    pass
    
            def test_dependents(self):
                    self.assertEqual(self.emp1.dependents, ["Caitlin MacNamara", "Llewelyn Edouard Thomas"])
                    self.emp2.addDependent("Jackson Pollock")
                    self.assertEqual(self.emp2.dependents, ["Jackson Pollock"])
                    self.assertEqual(self.emp3.dependents, [])
    
            if __name__ == '__main__':
                    unittest.main()
    
    
    And it is failing the assert on line self.assertEqual(self.emp3.dependents, [])

    The dependents of emp3 is not empty, it contains "Jackson Pollock", which is the dependent of emp2. The only way this could be true in my mind is if emp2 and emp3 are the same, but they are not (the employee names are different)

    Have put in print statements to help debug it and emp3 has no dependents until
    self.emp2.addDependent("Jackson Pollock") which seems to add Jackson Pollock to both emp2 and emp3

    Am really stumped here, so any help would be greatly appreciated.


Comments

Advertisement