Using __str__ representation for printing objects in containers in Python
- by BobDobbs
I've noticed that when an instance with an overloaded str method is passed to the print() function as an argument, it prints as intended. However, when passing a container that contains one of those instances to print(), it uses the repr method instead. That is to say, print(x) displays the correct string representation of x, and print(x, y) works correctly, but print([x]) or print((x, y)) prints the repr representation instead.
First off, why does this happen? Secondly, is there a way to correct that behavior of print() in this circumstance?