Using __repr__ or __str__ for own printable class?

Greg Ewing (using news.cis.dfn.de) ckea25d02 at sneakemail.com
Mon Apr 14 01:57:46 EDT 2003


Beni Cherniavsky wrote:
> Well, it is true that things will get confusing with strings.  But
> when you do an str() of a list, how much do you care for it?  Perhaps
> it's worth to apply str() in list.__str__, keeping list.__repr__ for
> the cases when you need an unambiguos representation?  I.e. leve it up
> to the user to decide whether he will be confused.

Python *does* leave it up to the user! Its containers
don't have a default __str__ at all, because there's
no way of knowing what the programmer wants it to
do.

If you care about the precise format of the string
representation you're trying to get for a container,
you shouldn't be using either str() or repr() --
you should be writing your own function that does
exactly what you want.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list