python3.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ======================
  2. Python 3 compatibility
  3. ======================
  4. Django 1.5 is the first version of Django to support Python 3. The same code
  5. runs both on Python 2 (≥ 2.6.5) and Python 3 (≥ 3.2), thanks to the six_
  6. compatibility layer and ``unicode_literals``.
  7. .. _six: http://packages.python.org/six/
  8. This document is not meant as a Python 2 to Python 3 migration guide. There
  9. are many existing resources, including `Python's official porting guide`_.
  10. Rather, it describes guidelines that apply to Django's code and are
  11. recommended for pluggable apps that run with both Python 2 and 3.
  12. .. _Python's official porting guide: http://docs.python.org/py3k/howto/pyporting.html
  13. Syntax requirements
  14. ===================
  15. Unicode
  16. -------
  17. In Python 3, all strings are considered Unicode by default. The ``unicode``
  18. type from Python 2 is called ``str`` in Python 3, and ``str`` becomes
  19. ``bytes``.
  20. You mustn't use the ``u`` prefix before a unicode string literal because it's
  21. a syntax error in Python 3.2. You must prefix byte strings with ``b``.
  22. In order to enable the same behavior in Python 2, every module must import
  23. ``unicode_literals`` from ``__future__``::
  24. from __future__ import unicode_literals
  25. my_string = "This is an unicode literal"
  26. my_bytestring = b"This is a bytestring"
  27. Be cautious if you have to `slice bytestrings`_.
  28. .. _slice bytestrings: http://docs.python.org/py3k/howto/pyporting.html#bytes-literals
  29. Exceptions
  30. ----------
  31. When you capture exceptions, use the ``as`` keyword::
  32. try:
  33. ...
  34. except MyException as exc:
  35. ...
  36. This older syntax was removed in Python 3::
  37. try:
  38. ...
  39. except MyException, exc:
  40. ...
  41. The syntax to reraise an exception with a different traceback also changed.
  42. Use :func:`six.reraise`.
  43. .. module: django.utils.six
  44. Writing compatible code with six
  45. ================================
  46. six is the canonical compatibility library for supporting Python 2 and 3 in
  47. a single codebase. Read its `documentation <six>`_!
  48. :mod:`six` is bundled with Django: you can import it as :mod:`django.utils.six`.
  49. Here are the most common changes required to write compatible code.
  50. String types
  51. ------------
  52. The ``basestring`` and ``unicode`` types were removed in Python 3, and the
  53. meaning of ``str`` changed. To test these types, use the following idioms::
  54. isinstance(myvalue, six.string_types) # replacement for basestring
  55. isinstance(myvalue, six.text_type) # replacement for unicode
  56. isinstance(myvalue, bytes) # replacement for str
  57. Python ≥ 2.6 provides ``bytes`` as an alias for ``str``, so you don't need
  58. :attr:`six.binary_type`.
  59. ``long``
  60. --------
  61. The ``long`` type no longer exists in Python 3. ``1L`` is a syntax error. Use
  62. :data:`six.integer_types` check if a value is an integer or a long::
  63. isinstance(myvalue, six.integer_types) # replacement for (int, long)
  64. ``xrange``
  65. ----------
  66. Import :func:`six.moves.xrange` wherever you use ``xrange``.
  67. Moved modules
  68. -------------
  69. Some modules were renamed in Python 3. The :mod:`django.utils.six.moves
  70. <six.moves>` module provides a compatible location to import them.
  71. In addition to six' defaults, Django's version provides ``dummy_thread`` as
  72. ``_dummy_thread``.
  73. PY3
  74. ---
  75. If you need different code in Python 2 and Python 3, check :data:`six.PY3`::
  76. if six.PY3:
  77. # do stuff Python 3-wise
  78. else:
  79. # do stuff Python 2-wise
  80. This is a last resort solution when :mod:`six` doesn't provide an appropriate
  81. function.