A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2005-August/055672.html below:

Allow str() to return unicode strings

[Python-Dev] Revised PEP 349: Allow str() to return unicode strings [Python-Dev] Revised PEP 349: Allow str() to return unicode stringsNeil 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4