How to print a list of user defined objects?

Hiya, I’d like to be able to print a list that contains my own types as easily as I can print a list of ints.

Here’s the code example:

class thing(object):
        def __init__(self, x):
               self.a = x

        def __str__(self):
                return str(self.a)

intlist = [int(n) for n in range(5)]
print intlist

thinglist = [thing(n) for n in range(5)]
print thinglist

When printing the list of my custom objects, it seems to just print the object rather than calling it’s str() method as I would like it to. Is there a way to make print use the string version of the objects in the list?

Grateful for your input.

ps. I know the list comprehension isn’t necessary to generate “intlist”, but I just learnt them and they’re fun : ).
pps. I know i could do:

for each in thinglist:
        print each,

but that’s not what I want.

Define a repr instead of a str method :wink:

Thanks alot. That was very concise and very useful.