printing a sequence...

Christopher T King squirrel at WPI.EDU
Fri Jul 16 16:09:20 CEST 2004


On Fri, 16 Jul 2004, Peter Otten wrote:

> Peter Otten wrote:
> 
> > Florian Preknya wrote:
> 
> >> What I want to obtain is [value: 10, value: 12, value: 2].
> 
> >> Can I write somehow the print statement to force calling the A.__str__
> >> function?
> > 
> > print [str(item) for item in a]
> 
> print "[%s]" % ", ".join(map(str, a))

I had the same problem with the same solution, and it makes me wonder, why
does a list's __str__() method call the __repr__() method of its members?  
Personally, I think the __str__() call should be propagated, not turned 
into a __repr__() call.  I can't think of any instances in which such 
behavior is what you want (if the __repr__() of the members is wanted, 
then __repr__() should be used).




More information about the Python-list mailing list