PARTH DUBEY
PARTH DUBEY

Reputation: 53

instance Variable not getting updated

why instance vaiable is not getting sorted when i use sort function, the function sort_players_based_on_experience(self) is giving None as o/p.

class Game:
    def __init__(self, players_list):
        self.__players_list=players_list

    def sort_players_based_on_experience(self) :


        self.__players_list=self.__players_list.sort(key=lambda x:x.get_experience())
        return self.__players_list

Upvotes: 0

Views: 22

Answers (1)

awarrier99
awarrier99

Reputation: 3855

.sort is an in-place function, and it doesn't return anything. What you want to do instead is just call .sort on the array without reassigning the variable, or use sorted which returns the sorted array:

def sort_players_based_on_experience(self) :
    self.__players_list.sort(key=lambda x: x.get_experience())
    return self.__players_list

or

def sort_players_based_on_experience(self) :
    self.__players_list = sorted(self.__players_list, key=lambda x: x.get_experience())
    return self.__players_list

Upvotes: 1

Related Questions