New submission from Steven D'Aprano <[email protected]>:
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.
----------
messages: 100984
nosy: stevenjd
severity: normal
status: open
title: String interpolation with unicode subclass fails to call __str__
versions: Python 2.5, Python 2.6, Python 3.1
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue8128>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com