1

I have to fill several objects with data and for this case I've created a dictionary where the key is a number-string and the value is a custom obj

my_dict = {
  '1': my_obj1,
  '2': my_obj2
}

my_obj looks like this

class MyObj:
    def __init__(self):
        self.title = ''
        self.num = 0
        self.name = ''


    def __str__(self):
        return 'title:' + self.title \
               + ', num:' + str(self.num) \
               + ', name:' + self.name

The problem is when I try to print it to the console. With print(my_dict['1']) I get the an expected output

title:here is a title, num:42, name:here is a name

but if I try to print the whole dict print(my_dict) it doesn't show me the content of Object

{'1': <__main__.MyObj object at 0x0000019C02A8E820>, '2': <__main__.MyObj object at 0x0000019C02A83820>}

But, why? Why it doesn't call the __str()__ method if you print the whole dict?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
BeJay
  • 425
  • 4
  • 15

0 Answers0