Re: string interpolation mystery in Python 2.6

Discussion in 'Python' started by Alan G Isaac, Sep 11, 2009.

  1. Alan G Isaac

    Alan G Isaac Guest

    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'"

    Cheers,
    Alan Isaac
     
    Alan G Isaac, Sep 11, 2009
    #1
    1. Advertising

  2. Alan G Isaac

    Chris Rebert Guest

    On Fri, Sep 11, 2009 at 3:12 PM, Alan G Isaac <> 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
     
    Chris Rebert, Sep 11, 2009
    #2
    1. Advertising

  3. On Fri, 11 Sep 2009 15:19:05 -0700, Chris Rebert wrote:

    > On Fri, Sep 11, 2009 at 3:12 PM, Alan G Isaac <>
    > wrote:
    >> Michael Foord came up with a much simpler illustration.  With Python
    >> 2.6::


    [snip]

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



    That's what it looks like to me too, which I wouldn't call either a bug
    or a feature. I don't think Python makes any promises regarding exception
    messages.

    However, I must admit I'm perplexed why the original example is calling
    __unicode__() in the first place! Given the line:

    raise self.severe('Problems with "%s" directive path:\n%s: %s.'
    % (self.name, error.__class__.__name__, error))

    it looks to me like it should be calling error.__str__() not
    error.__unicode(). Making the suggested edit:

    raise self.severe('Problems with "%s" directive path:\n%s: %s.'
    % (self.name, error.__class__.__name__, str(error)))

    should have no effect. But it (apparently) does. This brings us back to
    Alan's original question:

    "MYSTERY: how can "%s"%error be different from "%s"%str(error) in Python
    2.6?"



    --
    Steven
     
    Steven D'Aprano, Sep 12, 2009
    #3
  4. 2009/9/12 Steven D'Aprano <>:
    > On Fri, 11 Sep 2009 15:19:05 -0700, Chris Rebert wrote:
    >
    >> Sounds like IOError or one of its ancestors defines both __str__() and
    >> __unicode__ () special methods but has them produce different output.

    >
    >
    > That's what it looks like to me too, which I wouldn't call either a bug
    > or a feature. I don't think Python makes any promises regarding exception
    > messages.
    >
    >


    There are some report about the apparently same issue in the bug tracker.
    http://bugs.python.org/issue5274
    http://bugs.python.org/issue6108

    vbr
     
    Vlastimil Brom, Sep 12, 2009
    #4
  5. Alan G Isaac

    Alan G Isaac Guest

    On 9/11/2009 9:42 PM, Steven D'Aprano wrote:
    > However, I must admit I'm perplexed why the original example is calling
    > __unicode__() in the first place! Given the line:
    >
    > raise self.severe('Problems with "%s" directive path:\n%s: %s.'
    > % (self.name, error.__class__.__name__, error))
    >
    > it looks to me like it should be calling error.__str__() not
    > error.__unicode(). Making the suggested edit:
    >
    > raise self.severe('Problems with "%s" directive path:\n%s: %s.'
    > % (self.name, error.__class__.__name__, str(error)))
    >
    > should have no effect. But it (apparently) does. This brings us back to
    > Alan's original question:
    >
    > "MYSTERY: how can "%s"%error be different from "%s"%str(error) in Python
    > 2.6?"



    George Brandl explained it to me this way:

    It's probably best explained with a bit of code:

    >>> >>> class C(object):

    ... def __str__(self): return '[str]'
    ... def __unicode__(self): return '[unicode]'
    ...
    >>> "%s %s" % ('foo', C())

    'foo [str]'
    >>> "%s %s" % (u'foo', C())

    u'foo [unicode]'

    I.e., as soon as a Unicode element is interpolated into a string, further
    interpolations automatically request Unicode via __unicode__, if it exists.

    Pretty subtle ...

    Cheers,
    Alan Isaac
     
    Alan G Isaac, Sep 13, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aiden Humphreys

    Perl/Ruby string interpolation

    Aiden Humphreys, May 22, 2005, in forum: Java
    Replies:
    5
    Views:
    894
    Tor Iver Wilhelmsen
    May 22, 2005
  2. Paul Rubin
    Replies:
    4
    Views:
    307
    Michele Simionato
    Jan 19, 2004
  3. Michele Simionato

    yet another recipe on string interpolation

    Michele Simionato, Nov 4, 2004, in forum: Python
    Replies:
    8
    Views:
    409
    Raymond Hettinger
    Nov 8, 2004
  4. Kun
    Replies:
    2
    Views:
    425
    John J. Lee
    Apr 12, 2006
  5. Yingjie Lan

    Re: string interpolation for python

    Yingjie Lan, Apr 2, 2012, in forum: Python
    Replies:
    25
    Views:
    609
    alex23
    Apr 3, 2012
Loading...

Share This Page