> 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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to