Browse files

[py3] Explained @python_2_unicode_compatible usage

  • Loading branch information...
1 parent 4e68e86 commit 031896c5101de83bca65e872fb4a91c15f55a42e @aaugustin aaugustin committed Aug 12, 2012
Showing with 14 additions and 2 deletions.
  1. +14 −2 docs/topics/python3.txt
@@ -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_bytestring = b"This is a bytestring"
-If you need a byte string under Python 2 and a unicode string under Python 3,
-use the :func:`str` builtin::
+In classes, define ``__str__`` methods returning unicode strings and apply the
+: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')

0 comments on commit 031896c

Please sign in to comment.