Using __repr__ or __str__ for own printable class?

Christian Tanzer tanzer at swing.co.at
Mon Apr 14 04:02:57 EDT 2003


Alex Martelli <aleax at aleax.it> wrote:

> IMHO the most maddening use of __repr__ is that done by the
> implementation of __str__ for all built-in container types:
>
> >>> class X:
> ...   def __str__(self): return 'str'
> ...   def __repr__(self): return 'rep'
> ...
> >>> print [X()]
> [rep]
> >>>
>
> There's supposed to be a good reason for this to print [rep],
> rather than [str] as common sense would suggest, but I keep
> forgetting it (so it's probably nor very compelling;-).

You aren't seriously suggest the behavior

>>> print ["abc", "d[e]"]
[abc, d[e]]

, are you?

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list