[Python-Dev] __str__ and unicode

Fredrik Lundh fredrik at pythonware.com
Wed Dec 6 10:26:32 CET 2006

over at my work copy of the python language reference, Adrian Holovaty
asked about the exact semantics of the __str__ hook:


   "The return value must be a string object." Does this mean it can be a
   *Unicode* string object? This distinction is ambiguous to me because
    unicode objects and string objects are both subclasses of basestring.
    May a __str__() return a Unicode object?

I seem to remember earlier discussions on this topic, but don't recall when
and what.  From what I can tell, __str__ may return a Unicode object, but
only if can be converted to an 8-bit string using the default encoding.  Is this
on purpose or by accident?  Do we have a plan for improving the situation
in future 2.X releases ?


More information about the Python-Dev mailing list