str(list) calls repr() on elements?
donod at home.com
Thu Oct 19 04:25:05 CEST 2000
I'm not sure what you mean here. If the elements are not strings,
something has to convert them to strings before they can be printed. I
ran a test to see when str() or repr() is called:
>>> class nada:
... def __str__(self):
... return 'str'
... def __repr__(self):
... return 'repr'
>>> n = nada()
>>> print n
This seems to me to be undesirable behavior. I would expect that when
the str or repr functions are applied to sequences or mappings, that the
same (str or repr) function should be applied recursively to all
component elements. Should this be considered a bug?
Ulf Engström wrote:
> OKi, I'm not familiar with the interior of repr and str, but I believe none
> of the functions are called for each element of a list or a tuple, but
> rather stringifying the whole list.
> Am I wrong here?
> > It seems that the built-in function 'str', when applied to a list/tuple,
> > constructs its result by calling 'repr' on the elements of the list/tuple,
> > rather than 'str'. How come?
> > e.g.
> > >> str(1L)
> > '1'
> > >> repr(1L)
> > '1L'
> > >> str( [1L] )
> > '[1L]' # not ''
> > -Michael Dyck
> > --
> > http://www.python.org/mailman/listinfo/python-list
More information about the Python-list