[Python-3000] Displaying strings containing unicode escapes at the interactive prompt

Alex Martelli aleaxit at gmail.com
Thu Apr 17 06:59:08 CEST 2008


On Wed, Apr 16, 2008 at 6:53 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
   ...
>  >     open("тест") # filename is in koi8-r encoding
>  > IOError: [Errno 2] No such file or directory: '\xd4\xc5\xd3\xd4'
>
>  In that particular case, I'd say the IOError constructor
>  is doing the wrong thing -- it should be using something
>  like
>
>    "No such file or directory: '%s'" % filename\
>
>  instead of
>
>    "No such file or directory: %r" % filename
>
>  i.e. %r shouldn't be used as a quick and dirty way to
>  get a string quoted.

I disagree: I always recommend using %r to display (in an error
message, log entry, etc), a string that may be in error, NOT '%s',
because the cause of the error can often be that the string mistakenly
contains otherwise-invisible characters -- %r will show them clearly
(as escape sequences), while %s could hide them and lead anybody but
the most experienced developer to a long and frustrating debugging
session.


Alex


More information about the Python-3000 mailing list