Portable code: ‘from __future__ import unicode_literals’ a good idea? (was: Portable code: __import__ demands different string types between 2 and 3)

Ben Finney ben+python at benfinney.id.au
Mon Dec 15 19:42:04 EST 2014


Devin Jeanpierre <jeanpierreda at gmail.com> writes:

> On Sun, Dec 14, 2014 at 11:29 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> >     from __future__ import unicode_literals
>
> Ordinarily, for 2.x/3.3+ code I would suggest not doing this --
> instead, b'...' for bytes, u'...' for unicode, and '...' for native
> "string" type (bytes on 2.x, unicode on 3.x). This is the main benefit
> of the u'...' syntax addition.

That latter point would seemingly also apply to ‘from __future__ import
unicode_literals’, so is moot in this context.

As for the advice to avoid such a declaration, you're arguing against
the official guide for porting Python 2 code to 2-and-3 compatible code:

    For text you should either use the from __future__ import
    unicode_literals statement or add a u prefix to the text literal.

    <URL:https://docs.python.org/3.4/howto/pyporting.html#text-versus-binary-data>

So, the declarative import is specifically recommended. You'll need to
present a case for why I shouldn't follow that recommendation.

-- 
 \       “I disapprove of what you say, but I will defend to the death |
  `\     your right to say it.” —Evelyn Beatrice Hall, _The Friends of |
_o__)                                                  Voltaire_, 1906 |
Ben Finney




More information about the Python-list mailing list