Kaydet (Commit) 031896c5 authored tarafından Aymeric Augustin's avatar Aymeric Augustin

[py3] Explained @python_2_unicode_compatible usage

üst 4e68e861
...@@ -36,8 +36,20 @@ In order to enable the same behavior in Python 2, every module must import ...@@ -36,8 +36,20 @@ In order to enable the same behavior in Python 2, every module must import
my_string = "This is an unicode literal" my_string = "This is an unicode literal"
my_bytestring = b"This is a bytestring" my_bytestring = b"This is a bytestring"
If you need a byte string under Python 2 and a unicode string under Python 3, In classes, define ``__str__`` methods returning unicode strings and apply the
use the :func:`str` builtin:: :func:`~django.utils.encoding.python_2_unicode_compatible` decorator. It will
define appropriate ``__unicode__`` and ``__str__`` in Python 2::
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class MyClass(object):
def __str__(self):
return "Instance of my class"
If you need a byte string literal under Python 2 and a unicode string literal
under Python 3, use the :func:`str` builtin::
str('my string') str('my string')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment