How to apply __str__ function when printing a list of objects in Python
Solution 1:
Try:
class Test:
def __repr__(self):
return 'asd'
And read this documentation link:
Solution 2:
The suggestion in other answers to implement __repr__
is definitely one possibility. If that's unfeasible for whatever reason (existing type, __repr__
needed for reasons other than aesthetic, etc), then just do
print [str(x) for x in l]
or, as some are sure to suggest, map(str, l)
(just a bit more compact).