str() inconsistency

Mike Rovner mike at bindkey.com
Fri Jul 26 14:08:52 EDT 2002


Thank you, Mark.
Both posts are up to the point.

Fortunately, since 2.2 we can do:
>>> class L(list):
...  def __repr__(self): return '<L inst>'
...  def __str__(self): return '[%s]' % ', '.join([str(i) for i in self])
...
>>> l=L([1,2,3,4.1])
>>> l
<L inst>
>>> print l
[1, 2, 3, 4.1]
>>> print [1,2,3,4.1]
[1, 2, 3, 4.0999999999999996]

And even:
>>> list=L
>>> [1.1]
[1.1000000000000001]
>>> list([1.1])
<L inst>
>>> print list([1.1])
[1.1]

Unfortunately GvR consern remains:
>>> print list([1, '1', '2, 3', '], ['])
[1, 1, 2, 3, ], []

Sincerely,
Mike

"Mark McEahern" <marklists at mceahern.com> wrote in message
news:JHEOKEOOLIGLDHCMAHMOCEHODDAA.marklists at mceahern.com...
> [Mike Rovner]
> > Why it's not a bug:
> >
> > >>> 4.4
> > 4.4000000000000004
> > >>> `4.4`
> > '4.4000000000000004'
> > >>> str(4.4)
> > '4.4'
> > >>> str([4.4])
> > '[4.4000000000000004]'
> >
> > IMHO, str() shall call str, not repr on complex objects.
>
> Apparently, this topic comes up from time to time.  I was trying to find
the
> most recent post from Tim Peters on this (sometime in the last two weeks,
I
> believe).  It may be somewhere in the results of this search:
>
>
>
http://groups.google.com/groups?as_q=str%20repr%20list%20bug&as_ugroup=comp.
> lang.python
>
> I did find this one from October 2000:
>
>
>
http://groups.google.com/groups?selm=mailman.971929189.1785.python-list%40py
> thon.org
>
> as well as Guido's post on this topic from November 1999:
>
>
>
http://groups.google.com/groups?selm=5lln8ht38a.fsf%40eric.cnri.reston.va.us
>
> // mark
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>








More information about the Python-list mailing list