0

I am trying to return and then print the string version of values in a list, but it keeps printing only the Memory Address instead of the String itself.

To solve this issue I figured defining it as a string would be pretty straightforward, but I continue to have issues even after adding it. My code is below, any help would be appreciated!

class Statistic:
    def __init__(self, stat_name, stat_value, team_name):
        self.stat_name = stat_name
        self.stat_value = stat_value
        self.team_name = team_name
    def __str__(self):
        return Statistic(self.stat_name + ": " +self.stat_value + " - " + self.team_name)


run_stat_list = []
i = 0
while (i < 20):
    run_stat = Statistic("Runs", runvalueslist[i], runvalueslist[i + 1])
    run_stat_list.append(run_stat)
    i = i + 2

print(run_stat_list)
cpkell
  • 33
  • 3
  • `list.__str__` doesn't assume (or even check) that the elements of the list have a `__str__` method; it just uses their `__repr__` method instead to construct the string representation of the list. – chepner May 17 '22 at 00:49

1 Answers1

0

Your __str__ function is not returning a str but a new instance of the class.

Try instead:

def __str__(self):
    return self.stat_name + ": " + self.stat_value + " - " + self.team_name
Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15
  • 1
    While true, this doesn't help, because `list.__str__` doesn't try to call `Statistics.__str__` in the first place. – chepner May 17 '22 at 00:57