[Python-Dev] unicode and str (original) (raw)

M.-A. Lemburg mal at egenix.com
Tue Aug 31 20:58:31 CEST 2004


Neil Schemenauer wrote:

On Tue, Aug 31, 2004 at 10:23:33AM +0200, M.-A. Lemburg wrote:

str is indeed allowed to return Unicode objects (and has been for quite a while). [...] Now back to your original question: the change you see in %-formatting was actually a bug fix. Python 2.3 should have exposed the same behavior as 2.4 does now. I think the note in NEWS is not quite accurate. It says: Let u'%s' % obj try obj.unicode() first and fallback to obj.str(). The change in PyUnicodeJoin replaces a PyObjectStr() call with

I think you meant PyUnicode_Format().

PyObjectUnicode(). That means that this works in 2.4:

class A: def str(self): return u'\u1234' u'%s' % A() Perhaps that is unintentional but it seems cleaner to me than adding a unicode method and not providing a str method.

This is intended. However, you'll find that calling str(A()) will not give you a Unicode object, but an exception instead.

The reason is that even though the str method may pass back a Unicode object, PyObject_Str() still assures that its return value is a string (and that API is used by str()).

Basically, unicode is to unicode() what str is to str().

-- Marc-Andre Lemburg eGenix.com

Professional Python Services directly from the Source (#1, Aug 31 2004)

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 ! ::::



More information about the Python-Dev mailing list