Using __str__ representation for printing objects in containers in Python
Posted
by BobDobbs
on Stack Overflow
See other posts from Stack Overflow
or by BobDobbs
Published on 2010-04-14T02:44:12Z
Indexed on
2010/04/14
3:03 UTC
Read the original article
Hit count: 248
python
|operator-overloading
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?
© Stack Overflow or respective owner