0

Problem

How do I compare an object made from a class to a string? I've added multiple objects to a list. When I loop through the list:

for i in self._list:
   print(i)

It prints out this:

<sokker.Sokker object at 0x019E9A30>
martineau
  • 112,593
  • 23
  • 157
  • 280
Emil berg
  • 15
  • 3

2 Answers2

0

From what I can see, you want to print an object. You can use string to do that using:

list_of_objects = [my_object_one, my_object_two]
for object in list_of_objects:
  print(str(object))

It will call the __str__() of the object class and hopefully display the information you wish to see.

Sylhare
  • 4,330
  • 7
  • 50
  • 67
0

Here there is a similar question.

Just to resume, to print object it's class should have method __str__() defined, like here:

class Test:
    def __str__(self):
        return "my custom text"
dmigo
  • 2,588
  • 3
  • 36
  • 58