Newbie question: how to join a list of elements of user-defined types?

Fortepianissimo fortepianissimo at yahoo.com.tw
Mon Apr 29 21:54:12 EDT 2002


This question is so fundamental that I strongly suspect it's already
answered - ig that's the case, please forgive this poor newbie, for he
has done some work searching over Python FAQ and Google.

Basically I want to have a subclass of list, like

class MyList (list):
    ...

then I want to

l=MyList()
... do stuff to fill l
s=" ".join(l)

with the hope that s will be a nicely joined string with " " as the
delimiter. Given my C++ background, I tried to define __str__/__repr__
for both MyList and the user-defined types of the list members,
assuming the type conversion would silently be invoked. But I was
wrong!

Even trying this failed:

l=[1,2,3]
s=" ".join(l)

and I thought built-in types all have default __str__ and __repr__?

What am I missing here? Many thanks to someone who would take time to
rescue a poor soul.


Ben



More information about the Python-list mailing list