'Python: TypeError: unbound method insertionSort() must be called with Employee instance as first argument (got list instance instead) [duplicate]

So I can figure out what is the meaning of TypeError: unbound method insertionSort() must be called with Employee instance as first argument (got list instance instead) Please help!

class Employee():    

def __init__(self, name, salary):
    self.name = name
    self.salary = salary


def insertionSort(EmpList):
    for i in range (1, len(EmpList)):
        key = EmpList[i]
        j = i - 1
        while j >= 0 and key < EmpList[j]:
            EmpList[j + 1] = EmpList[j]
            j -= 1
        EmpList[j + 1] = key

 
e1 = Employee("Ada", 15000)
e2 = Employee("Brian", 18000)
e3 = Employee("Carson", 12000)
e4 = Employee("Dave", 14000)
EmpList = [e1,e2,e3,e4]
Employee.insertionSort(EmpList)
for i in range (len(EmpList)):
    print("% d" % EmpList[i])


Solution 1:[1]

class Employee():    

  def __init__(self, name, salary):
      self.name = name
      self.salary = salary


  def insertionSort(EmpList):
      for i in range (1, len(EmpList)):
          key = EmpList[i]
          j = i - 1
          while j >= 0 and key.salary < EmpList[j].salary:
              EmpList[j + 1] = EmpList[j]
              j -= 1
          EmpList[j + 1] = key

 
e1 = Employee("Ada", 15000)
e2 = Employee("Brian", 18000)
e3 = Employee("Carson", 12000)
e4 = Employee("Dave", 14000)
EmpList = [e1,e2,e3,e4]
Employee.insertionSort(EmpList)
for i in range (len(EmpList)):
    print(EmpList[i].name,EmpList[i].salary)

You can't compare instances of Emloyee, instead I changed it "key.salary < EmpList[j].salary" so you can compare their salary

Solution 2:[2]

You're getting this error since you didn't add @staticmethod above def insertionSort(... read more

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    @staticmethod
    def insertionSort(EmpList):
        for i in range(1, len(EmpList)):
            key = EmpList[i]
            j = i - 1
            while j >= 0 and key.salary < EmpList[j].salary:
                EmpList[j + 1] = EmpList[j]
                j -= 1
            EmpList[j + 1] = key


e1 = Employee("Ada", 15000)
e2 = Employee("Brian", 18000)
e3 = Employee("Carson", 12000)
e4 = Employee("Dave", 14000)
EmpList = [e1, e2, e3, e4]
Employee.insertionSort(EmpList)
for i in range(len(EmpList)):
    print(EmpList[i].name)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PyotrVanNostrand
Solution 2 Jossef Harush Kadouri