-1

I am generating a list of employees and managers. However, I obtained this weird output after trying sort them by last name (my initial class employee only contains "name" but not divide into first name and last name. Then, I use [1] to indicate the last name). What's wrong with my code since I can't see my list of employees.

class Employee:
    def __init__(self, name, socialSecurityNumber, salary):
        """
        Set name, socialSecurityNumber, and salary to itself.
        """
        self.name = name
        self.socialSecurityNumber = socialSecurityNumber
        self.salary = salary

employeeList = []
employee1 = Employee("Banny Chu", "777-88-9999", 45000)
employee2 = Employee("Luffy Monkey", "555-66-9999", 32000)
employee3 = Employee("Zoro Nonoroa", "222-00-3333", 37000)
manager1 = Manager("Scalt Haight", "444-33-1111", 60000, "Lab", 2300)
manager2 = Manager("Kapu Ro", "333-44-2222", 65000, "General", 2600)
manager3 = Manager("Nami Swan", "111-77-6666", 80000, "HR", 3000)
employeeList.append(employee1)
employeeList.append(employee2)
employeeList.append(employee3)
employeeList.append(manager1)
employeeList.append(manager2)
employeeList.append(manager3)

print (sorted(employeeList, key=lambda employee: employee.name[1].lower()))

Output as below (strange output since I can't see my employeeList in the correct format even though I type print(employeeList) and gave the same format as below.

[<employee8.Employee object at 0x105a48b00>, <manager8.Manager object at 0x1054290f0>, <manager8.Manager object at 0x1054290f0>, <manager8.Manager object at 0x1054290f0>, <manager8.Manager object at 0x1054290f0>]

What should I modify it so that I can see my sorted list in the way that I can clearly see them?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Wu Chu
  • 5
  • 3
  • Why is it 'weird'? It is the default representation for any custom Python class. You'd get the same if you used `print(employee1)` for example. What did you expect to be output instead? – Martijn Pieters Feb 28 '15 at 18:57
  • I am trying to sort my list of employees and mangers by the last name. If I can't see them, how do I know my sorting code is working fine? – Wu Chu Feb 28 '15 at 18:59
  • You shouldn't append to a global list individually, you should have a list defined as a member of your class to which each object appends itself in your initializer. Agreed @MartijnPieters? – Malik Brahimi Feb 28 '15 at 19:12
  • @MalikBrahimi: Not really. – Martijn Pieters Feb 28 '15 at 19:13
  • @MartijnPieters What would be the point of self managed Employee objects? This is the essence of object oriented programming. I find this mess outside of the class to be very inefficient. – Malik Brahimi Feb 28 '15 at 19:16
  • 1
    @MalikBrahimi: I disagree with that. It is not the responsibility of the `Employee` class to dictate and handle storage. What if I wanted to use multiprocessing and send class data over to the other process? I don't want there to be a list there too, I just want there to be one instance. – Martijn Pieters Feb 28 '15 at 19:21

3 Answers3

2

By default, user-defined objects will be represented as a class instance at a location in memory:

<__main__.Employee instance at 0x02A39940>

You will need to add a special method for object representation in your Employee class:

class Employee:
    def __init__(self, name, socialSecurityNumber, salary):
        """
        Set name, socialSecurityNumber, and salary to itself.
        """
        self.name = name
        self.socialSecurityNumber = socialSecurityNumber
        self.salary = salary

    def __repr__(self):
        return self.name # represents object with name
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

You're missing the point that sorted returns a permutation of the list that got sorted based on the criteria you sent it. It doesn't automagically return just the keys you sorted them on if that's what you were expecting?

sort = sorted(employeeList, key=lambda employee: employee.name[1].lower())
print([emp.name.split()[1] for emp in employeeList])

Output (I was lazy and only copy pasted 3 of your employees):

['Chu', 'Monkey', 'Nonoroa']

You were also missing a split, because you save your name in a single string. Indexing a single string will return a single character at that location in a string.

If your goal wasn't to print out just the last names, then you have to override either the __str__ or __repr__ method. (Read about what exact difference is between the methods here.)

Community
  • 1
  • 1
ljetibo
  • 3,048
  • 19
  • 25
  • Yes, I also want to print out everything but sort it by the last name. How do I print out the code with all the info but not just the last name? I read the link, but not quite get it. – Wu Chu Feb 28 '15 at 19:40
  • @WuChu Then you have to def the __str__ method as defined in tge lunk – ljetibo Feb 28 '15 at 20:30
0

You forgot to split the name in the key function: employee.name.split(' ')[1]. Python calls __repr__ on the sorted list which prints '[' and ']' at the beginning and end and then calls __repr__ on each list element. The default __repr__ prints the object type and address. If you want to see something else, you have to give python another __repr__ to call.

class Employee:
    def __init__(self, name, socialSecurityNumber, salary):
        """
        Set name, socialSecurityNumber, and salary to itself.
        """
        self.name = name
        self.socialSecurityNumber = socialSecurityNumber
        self.salary = salary

    def __repr__(self):
        return "{} {} {}".format(self.name, self.socialSecurityNumber, self.salary)

class Manager(Employee):
    def __init__(self, name, socialSecurityNumber, salary, department, unknown):
        super().__init__(name, socialSecurityNumber, salary)



employeeList = []
employee1 = Employee("Banny Chu", "777-88-9999", 45000)
employee2 = Employee("Luffy Monkey", "555-66-9999", 32000)
employee3 = Employee("Zoro Nonoroa", "222-00-3333", 37000)
manager1 = Manager("Scalt Haight", "444-33-1111", 60000, "Lab", 2300)
manager2 = Manager("Kapu Ro", "333-44-2222", 65000, "General", 2600)
manager3 = Manager("Nami Swan", "111-77-6666", 80000, "HR", 3000)
employeeList.append(employee1)
employeeList.append(employee2)
employeeList.append(employee3)
employeeList.append(manager1)
employeeList.append(manager2)
employeeList.append(manager3)

for employee in sorted(employeeList, key=lambda employee: employee.name.split(' ')[1].lower()):
    print(employee)
Harvey
  • 5,703
  • 1
  • 32
  • 41