class Player:
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return self.name +" " +str(self.score)
n = int(input())
data = []
for i in range(n):
name, score = input().split()
score = int(score)
player = Player(name,score)
data.append(player)
sorted(data,key=getattr(player.score,'name'))
for i in range(n):
print(data[i])
I am trying to sort a list by using the getattr
function. When I try to pass the object in the arguments list I get the following error:
Traceback (most recent call last):
File "C:/Users/raghu/.PyCharmCE2018.2/config/scratches/demo2.py", line 15, in <module> sorted(data,key=getattr(player.score,'name'))
AttributeError: 'int' object has no attribute 'name'