[Python-Dev] Revised PEP 349: Allow str() to return unicode strings

Neil Schemenauer nas at arctrix.com
Thu Aug 25 19:03:32 CEST 2005


On Wed, Aug 24, 2005 at 09:11:18PM +0200, Dieter Maurer wrote:
> Neil Schemenauer <nas at arctrix.com> writes on Mon, 22 Aug 2005 15:31:42 -0600:
> >     The code was fixed by changing
> >     the line "header = str(header)" to:
> > 
> >         if isinstance(header, unicode):
> >             header = header.encode('ascii')
> 
> Note, that this is not equivalent to the old "str(header)":
> 
>   "str(header)" used Python's "default encoding" while the
>   new code uses 'ascii'.

It also doesn't call __str__ if the object is not a basestring
instance.  I have a hard time understanding the exact purpose of
calling str() here.  Maybe Barry can comment.

> Can we get a new builtin with the exact same behaviour as
> the current "str" which can be used when we do require an "str"
> (and cannot use a "unicode").

That fact that no code in the standard library requires such a
function (AFAIK), leads me to believe that it would not be useful
enough to be made a built-in.  You would just write it yourself:

    def mystr(s):
        s = str(s)
        if isinstance(s, unicode):
            s = s.encode(sys.getdefaultencoding())
        return s

Cheers,

  Neil


More information about the Python-Dev mailing list