This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients steven.daprano
Date 2010-03-13.02:47:31
SpamBayes Score 2.1543954e-07
Marked as misclassified No
Message-id <1268448454.93.0.643768410132.issue8128@psf.upfronthosting.co.za>
In-reply-to
Content
String interpolation % operates on unicode strings directly without calling the __str__ method. In Python 2.5 and 2.6:

>>> class K(unicode):
...     def __str__(self): return "Surprise!"
...
>>> u"%s" % K("some text")
u'some text'

but subclasses of str do call __str__:

>>> class K(str):
...     def __str__(self): return "Surprise!"
...
>>> "%s" % K("some text")
'Surprise!'

In Python 3.1, the above example for subclassing str operates like the unicode example, i.e. it fails to call __str__.

The documentation for string interpolation states that str() is called for all Python objects.

http://docs.python.org/library/stdtypes.html#string-formatting-operations

If the behaviour for unicode (2.5/2.6, str in 3.1) is considered correct, then the documentation should be updated to say that unicode is an exception to the rule. Otherwise the behaviour is incorrect, and it should call __str__ the same as everything else.
History
Date User Action Args
2010-03-13 02:47:35steven.dapranosetrecipients: + steven.daprano
2010-03-13 02:47:34steven.dapranosetmessageid: <1268448454.93.0.643768410132.issue8128@psf.upfronthosting.co.za>
2010-03-13 02:47:32steven.dapranolinkissue8128 messages
2010-03-13 02:47:31steven.dapranocreate