Message100984
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. |
|
Date |
User |
Action |
Args |
2010-03-13 02:47:35 | steven.daprano | set | recipients:
+ steven.daprano |
2010-03-13 02:47:34 | steven.daprano | set | messageid: <1268448454.93.0.643768410132.issue8128@psf.upfronthosting.co.za> |
2010-03-13 02:47:32 | steven.daprano | link | issue8128 messages |
2010-03-13 02:47:31 | steven.daprano | create | |
|