[Python-3000] PEP 3138- String representation in Python 3000
Jim Jewett
jimjjewett at gmail.com
Tue May 27 04:13:10 CEST 2008
On 5/26/08, Adam Olsen <rhamph at gmail.com> wrote:
> On Mon, May 26, 2008 at 5:24 PM, Oleg Broytmann <phd at phd.pp.ru> wrote:
> > There are two different and unrelated problems. One is that
> > str(container) calls repr() on items. This probably could be fixed with
> > a flag to repr() so it remembers it was called from str(). This has nothing
> > with hex-encoding strings - calling str() on items would be a win in any
> > case, especially for items that implements both __str__ and __repr__
> > methods.
> There's a reason for that convention. Would you prefer str(['1', '2',
> '3']) return '[1, 2, 3]'?
I don't think anyone is arguing about how to display
>>> "%" % string
The problem is classes where str(x) != repr(x), and how they get
messed up when a container holding (one of their) instances is
printed.
>>> class A:
def __str__(self): return "an A"
>>> a=A()
>>> print a # this is fine.
an A
>>> str(a) # this is OK, you have asked for "%s" % a
'an A'
>>> repr(a) # this is OK, you wanted repr explicitly.
'<__main__.A instance at 0x012DDAF8>'
>>> print ([a]) # this stinks ...
[<__main__.A instance at 0x012DDAF8>]
It would be much better as:
>>> print ([a]) # after fixing the recursion bug
['an a']
Whereas you are asking about the (perhaps also acceptable):
>>> # after fixing the recursion bug,
>>> print ([a]) # and somehow not even applying str
[an a]
-jJ
More information about the Python-3000
mailing list