0

if I run this programm in order to sort my list after the attribute "name", I only receive this:

[<__main__.Gericht object at 0x00000190D292FE50>, <__main__.Gericht object at 0x00000190D292FD90>, <__main__.Gericht object at 0x00000190D292FEB0>, <__main__.Gericht object at 0x00000190D292FDF0>]

Could you help me to get the real results?

class Gericht:

    def __init__(self, name, preis, calories, alergene = []):
        self.name = name
        self.preis = preis
        self.calories = calories
        self.alergene = alergene

def sortbyName(input):
    sort = sorted(input, key=lambda gericht: gericht.name)
    return sort

speisekarte = [Gericht("Spaghetti", 7.5, 750, [1, "a"]), Gericht("Brotstulle", 3, 250, []), Gericht("Tofu", 13, 634, [1, 2, 3, "b"]), Gericht("Eis", 1, 120, ["c", "d"]) ]

print(sortbyName(speisekarte))
buran
  • 13,682
  • 10
  • 36
  • 61

1 Answers1

0

Add something like:

    def __repr___(self) -> str:
        return "Some Gereicht object"

to your Gereicht class. Modify to taste until you get the output you want from your printed list.

Samwise
  • 68,105
  • 3
  • 30
  • 44