[Python-Dev] String formatting / unicode 2.5 bug?

Nick Coghlan ncoghlan at gmail.com
Sun Aug 20 07:03:21 CEST 2006


John J Lee wrote:
> Is this a bug?

I don't believe so - the string formatting documentation states that the 
result will be unicode if either the format string is unicode or any of the 
objects passed to a %s format code is unicode.

That latter part has just been extended to include any object that returns 
Unicode from __str__, instead of being restricted to actual Unicode instances.

Note that the following behaves the same way regardless of whether you use 2.4 
or 2.5:
"%s" % 'hi'
"%s" % u'hi'

And once the result has been promoted to unicode, __unicode__ is used directly:

 >>> print repr("%s%s" % (a(), a()))
__str__
accessing <__main__.a object at 0x00AF66F0>.__unicode__
__str__
accessing <__main__.a object at 0x00AF6390>.__unicode__
__str__
u'hihi'

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list