string interpolation mystery in Python 2.6

Chris Rebert clp2 at rebertia.com
Fri Sep 11 18:19:05 EDT 2009


On Fri, Sep 11, 2009 at 3:12 PM, Alan G Isaac <alan.isaac at gmail.com> wrote:
> Michael Foord came up with a much simpler
> illustration.  With Python 2.6::
>
>        >>> try:
>        ...  open('flooble')
>        ... except Exception as e:
>        ...  pass
>        ...
>        >>> e
>        IOError(2, 'No such file or directory')
>        >>> unicode(e)
>        u"(2, 'No such file or directory')"
>        >>> str(e)
>        "[Errno 2] No such file or directory: 'flooble'"
>        >>> u'%s' % e
>        u"(2, 'No such file or directory')"
>        >>> '%s' % e
>        "[Errno 2] No such file or directory: 'flooble'"

Sounds like IOError or one of its ancestors defines both __str__() and
__unicode__ () special methods but has them produce different output.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list