[Python-3000] PEP 3138- String representation in Python 3000

Oleg Broytmann phd at phd.pp.ru
Tue May 27 01:24:02 CEST 2008


On Mon, May 26, 2008 at 06:53:41PM -0400, Jim Jewett wrote:
> I certainly support a flag for repr meaning "This was really str; repr
> got called because the container doesn't have str, but go back to str
> for the contents."  (Alternatively, write an explicit repr that does
> that, add it to the builtin types, and make it available for easy use
> with extensions.)
> 
> > Exceptions use repr() for file names,
> >  e.g., which is very inconvenient, IMHO.
> 
> I'm not sure I fully understand this problem, but I would expect the
> right solution to be a change to either Exception.__str__ or the way
> filename-related exceptions are initialized.  Changing all of repr is
> again overkill.

   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.
   The other problem is that repr(string) returns it hex-encoded. The second
problem is hard to fix without changing repr itself, because repr() is used
in many places (exceptions was only one example). On the other hand those
who want the old (current) behaviour can do
repr(obj).encode("ascii", errors="backslashreplace").

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.


More information about the Python-3000 mailing list