Home>

There is a code:

class Object:
def __init __ (self, name, value):
    self.name= name
    self.value= value
def __str __ (self):
    return str (self.name) + ":" + str (self.value)
# List -an array of object references
test_list= [Object ("Name", 32), 3405, "hello"]
print (test_list) # Prints -[<
__main __. Object object at 0x00000139E226FFD0 >
, 3405, 'hello']
test_list= list (map (str, test_list))
print (test_list) # Prints -['Name: 32', '3405', 'hello']

Question:

  1. Why isn't the object cast to a string, although the str method is implemented? Separately, not in the list, the object is successfully coerced to the string ('Name: 32')
  • Answer # 1

    Because when printing a container (thanks to GrAnd for the clarification), another method is used, which must be implemented in the class:

    def __repr __ (self):
            return str (self)
    

    You have not finished your question about sorting, but you also need to implement some class methods there. See, in modern python it is enough to implement just a couple of methods -one comparison and one equality and decorate the class with a special decorator, python will do the rest itself.

    Generally interesting ... print () actually uses __str__. But if the container is being printed, then __repr__. A long time ago there was a proposal to bring everything to a single form, but it was rejected.

    GrAnd2021-11-24 18:24:59

    @GrAnd Hmm, cool, yes. If you do print (* test_list), then __str__ will be used, funny.

    CrazyElf2021-11-24 18:28:52

    I solved the sorting issue myself, but thanks for the clarification. When changing str to repr, everything worked successfully, thanks!

    Виталий Милонов2021-11-24 19:26:19

    Better to implement both methods, i.e. add the one I wrote to yours :)

    CrazyElf2021-11-25 05:02:51
  • Answer # 2

    Because when printing a container (thanks to GrAnd for the clarification), another method is used, which must be implemented in the class:

    def __repr __ (self):
            return str (self)
    

    You have not finished your question about sorting, but you also need to implement some class methods there. See, in modern python it is enough to implement just a couple of methods -one comparison and one equality and decorate the class with a special decorator, python will do the rest itself.

    Generally interesting ... print () actually uses __str__. But if the container is being printed, then __repr__. A long time ago there was a proposal to bring everything to a single form, but it was rejected.

    GrAnd2021-11-24 18:24:59

    @GrAnd Hmm, cool, yes. If you do print (* test_list), then __str__ will be used, funny.

    CrazyElf2021-11-24 18:28:52

    I solved the sorting issue myself, but thanks for the clarification. When changing str to repr, everything worked successfully, thanks!

    Виталий Милонов2021-11-24 19:26:19

    Better to implement both methods, i.e. add the one I wrote to yours :)

    CrazyElf2021-11-25 05:02:51