[Python-Dev] unicode and __str__
Neil Schemenauer
nas at arctrix.com
Mon Aug 30 23:23:18 CEST 2004
On Mon, Aug 30, 2004 at 10:57:36PM +0200, "Martin v. L?wis" wrote:
> It's what unicode() returns:
But unicode() will also return __str__, eg.
>>> class A:
... def __str__(self):
... return u'\u1234'
...
>>> unicode(A())
u'\u1234'
Why would I want to use __unicode__? Shouldn't we be heading to a
world where __str__ always returns unicode objects?
Now, say your class stores unicode character data. You could have
__unicode__ return a unicode object and have __str__ encode it
somehow and return a str. However, that seems like a horrible
design to me. The class can't know what kind of encoding the caller
expects.
Neil
More information about the Python-Dev
mailing list