2

I have a set of objects that I want to print in readable form. The objects' class overrides __str__, which should be used for printing. However, according to this post, __repr__ is used when printing containers, which is not what I want.

The posts advice to use print "["+", ".join(l)+"]" for lists doesn't work for sets. So what can I do?

Community
  • 1
  • 1
stefanbschneider
  • 5,460
  • 8
  • 50
  • 88
  • 1
    What do you mean, it doesn't work for sets? It'll have square brackets that are kind of misleading, but it should work just as well for sets as it does for lists (assuming you have strings in there). – TigerhawkT3 Oct 10 '16 at 10:10
  • 1
    Briefly: `print(*myset)` or `print(', '.join(map(str, myset)))`. – TigerhawkT3 Oct 10 '16 at 10:13
  • Thanks, `print(', '.join(map(str, myset)))` works. But `print("{"+", ".join(myset)+"}")` doesn't because it doesn't contain strings? – stefanbschneider Oct 10 '16 at 10:36
  • That's right. `print` can print anything, but `str.join` only works on strings. – TigerhawkT3 Oct 10 '16 at 10:37

0 Answers0