-1

I have coded a class that stores a name string and an int ratings ("John", 6) and stored it in a list "teams = []"

class player():
    def __init__(self, name, rating):
        self.name = name
        self.rating = rating

teams = []

teams.append(player("juanma", 6))
teams.append(player("pablo", 7))
teams.append(player("gon", 5))
teams.append(player("pep", 4))

I have then used the combinations tool from itertools and I am trying to get it to print all the possible combinations. The problem is that it is printing the memory allocation instead of the variables.

comb = combinations(teams, 2)

for i in comb:
    print(i)

This is the output that I get:

(<__main__.player object at 0x7fc4999698b0>, <__main__.player object at 0x7fc499969610>)
(<__main__.player object at 0x7fc4999698b0>, <__main__.player object at 0x7fc499999e20>)
(<__main__.player object at 0x7fc4999698b0>, <__main__.player object at 0x7fc499a015e0>)
(<__main__.player object at 0x7fc499969610>, <__main__.player object at 0x7fc499999e20>)
(<__main__.player object at 0x7fc499969610>, <__main__.player object at 0x7fc499a015e0>)
(<__main__.player object at 0x7fc499999e20>, <__main__.player object at 0x7fc499a015e0>)
  • What were you expecting to see instead? You haven't defined a `.__str__()` or `.__repr__()` method on your class to give it any particular string representation. – jasonharper Jun 09 '21 at 17:41

3 Answers3

1

YOU have to provide this.

class player():
    def __init__(self, name, rating):
        self.name = name
        self.rating = rating
    def __str__(self):
        return "<player {} {}>".format(self.name,self.rating)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

You need to define an __repr__ or __str__ internal function for your player class:

class player():
    def __init__(self, name, rating):
        self.name = name
        self.rating = rating
    def __str__(self):
        return self.name # Or any other string representation

You can find a nice explanation of the difference between these two methods here: What is the difference between __str__ and __repr__?

Ayush Garg
  • 2,234
  • 2
  • 12
  • 28
0

You need to add a .__str__() method on your class:

def __str__(self):
  return "Name: " + self.name + " Rating: " + str(self.rating)
i-am-cal
  • 16
  • 1