Using __str__ after sorting

Home / python / Using __str__ after sorting

I’ve created a list using class Photo and then sorted it into another list. I then want to print the string representation for just the first three items in the list. With the code I’ve written, I end up with a blank version of the string (like "Title:" instead of "Title: Volcano"). Where am I going wrong?

Here’s my code:class Photo :
def __init__(self,p_d) :self.title = p_d["p"]["t"]["c"]
self.tags = p_d["p"]["ts"]["t"]

def t_c(self):tot = 0
for t in self.tags:tot = tot + 1
return tot

def __str__(self):return "Title: {}nTags: {}".format(self.title,self.tags)

p_i = []
for d in p_d_l:d2 = Photo(d)

sorted_p = []
for d in sorted(p_i, key = Photo.t_c):sorted_p.append(Photo(d))
for d in sorted_p[:3]:print(d)


Read more

Leave a Reply

Your email address will not be published. Required fields are marked *