On 2006-12-06 10:46, M.-A. Lemburg wrote: > On 2006-12-06 10:26, Fredrik Lundh wrote: >> over at my work copy of the python language reference, Adrian Holovaty >> asked about the exact semantics of the __str__ hook: >> >> http://effbot.org/pyref/__str__ >> >> "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 ? > > This was added to make the transition to all Unicode in 3k easier: > > .__str__() may return a string or Unicode object. > > .__unicode__() must return a Unicode object. > > There is no restriction on the content of the Unicode string > for .__str__().
One more thing, since these two hooks are commonly used with str() and unicode(): * unicode(obj) will first try .__unicode() and then revert to .__str__() (possibly converting the string return value to Unicode) * str(obj) will try .__str__() only (possibly converting the Unicode return value to a string using the default encoding) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Dec 06 2006) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! :::: _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com