diff options
| author | Claude Paroz <claude@2xlibre.net> | 2014-04-26 16:00:15 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2014-04-26 16:03:40 +0200 |
| commit | e441cebce340f54741be957817cc034000deab3c (patch) | |
| tree | 0a1b9730a86f6a3660961e220cd099494f9fd8f2 /docs/topics/python3.txt | |
| parent | abd68b5affaec35e941b49f1b0a4cb8d70c22d7b (diff) | |
[1.7.x] Updated doc links to point to Python 3 documentation
Backport of 680a0f08b from master.
Diffstat (limited to 'docs/topics/python3.txt')
| -rw-r--r-- | docs/topics/python3.txt | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/docs/topics/python3.txt b/docs/topics/python3.txt index 15c07ccbf0..c10f2b42ab 100644 --- a/docs/topics/python3.txt +++ b/docs/topics/python3.txt @@ -78,8 +78,8 @@ wherever possible and avoid the ``b`` prefixes. String handling --------------- -Python 2's :func:`unicode` type was renamed :func:`str` in Python 3, -:func:`str` was renamed ``bytes()``, and :func:`basestring` disappeared. +Python 2's `unicode`_ type was renamed :class:`str` in Python 3, +``str()`` was renamed :func:`bytes`, and `basestring`_ disappeared. six_ provides :ref:`tools <string-handling-with-six>` to deal with these changes. @@ -131,35 +131,34 @@ and ``SafeText`` respectively. For forwards compatibility, the new names work as of Django 1.4.2. -:meth:`~object.__str__` and :meth:`~object.__unicode__` methods ---------------------------------------------------------------- +:meth:`~object.__str__` and ` __unicode__()`_ methods +----------------------------------------------------- In Python 2, the object model specifies :meth:`~object.__str__` and -:meth:`~object.__unicode__` methods. If these methods exist, they must return +` __unicode__()`_ methods. If these methods exist, they must return ``str`` (bytes) and ``unicode`` (text) respectively. -The ``print`` statement and the :func:`str` built-in call +The ``print`` statement and the :class:`str` built-in call :meth:`~object.__str__` to determine the human-readable representation of an -object. The :func:`unicode` built-in calls :meth:`~object.__unicode__` if it +object. The ``unicode`` built-in calls ` __unicode__()`_ if it exists, and otherwise falls back to :meth:`~object.__str__` and decodes the result with the system encoding. Conversely, the :class:`~django.db.models.Model` base class automatically derives -:meth:`~object.__str__` from :meth:`~object.__unicode__` by encoding to UTF-8. +:meth:`~object.__str__` from ` __unicode__()`_ by encoding to UTF-8. In Python 3, there's simply :meth:`~object.__str__`, which must return ``str`` (text). -(It is also possible to define ``__bytes__()``, but Django application have -little use for that method, because they hardly ever deal with -``bytes``.) +(It is also possible to define :meth:`~object.__bytes__`, but Django application +have little use for that method, because they hardly ever deal with ``bytes``.) Django provides a simple way to define :meth:`~object.__str__` and -:meth:`~object.__unicode__` methods that work on Python 2 and 3: you must +` __unicode__()`_ methods that work on Python 2 and 3: you must define a :meth:`~object.__str__` method returning text and to apply the :func:`~django.utils.encoding.python_2_unicode_compatible` decorator. On Python 3, the decorator is a no-op. On Python 2, it defines appropriate -:meth:`~object.__unicode__` and :meth:`~object.__str__` methods (replacing the +` __unicode__()`_ and :meth:`~object.__str__` methods (replacing the original :meth:`~object.__str__` method in the process). Here's an example:: from __future__ import unicode_literals @@ -233,7 +232,7 @@ In order to enable the same behavior in Python 2, every module must import my_bytestring = b"This is a bytestring" If you need a byte string literal under Python 2 and a unicode string literal -under Python 3, use the :func:`str` builtin:: +under Python 3, use the :class:`str` builtin:: str('my string') @@ -402,3 +401,7 @@ extras. In addition to six' defaults moves, Django's version provides ``thread`` as ``_thread`` and ``dummy_thread`` as ``_dummy_thread``. + +.. _unicode: http://docs.python.org/2/library/functions.html#unicode +.. _ __unicode__(): https://docs.python.org/2/reference/datamodel.html#object.__unicode__ +.. _basestring: http://docs.python.org/2/library/functions.html#basestring |
