[Python-Dev] readd u'' literal support in 3.3?

"Martin v. Löwis" martin at v.loewis.de
Fri Dec 9 09:32:03 CET 2011


> Or, alternatively, you use 'six' (or a similar compatibility module)
> and ensure unicode at runtime, using native or binary strings
> otherwise:
> 
> ----------
> from six import u
> foo = u("this is a Unicode string in both Python 2.x and 3.x")
> bar = "this is an 8-bit string in Python 2.x and a Unicode string in 3.x"
> baz = b"this is an 8-bit string in Python 2.x and a bytes object in 3.x"
> ----------

An alternative here is to use a function for bar, not foo:

from __future__ import unicode_literals
from six.next import native_str
foo = "this is a Unicode string in both Python 2.x and 3.x"
bar = native_str("this is an 7-bit string in Python 2.x"
                 " and a Unicode string in 3.x")
baz = b"this is an 8-bit string in Python 2.x and a bytes object in 3.x"

Which of them is "better" depends on which of the two string types are
more common.

Regards,
Martin


More information about the Python-Dev mailing list