{"title":"Porting to Python 3","version":"1.10","locale":"ja","docname":"topics/python3","url":"/ja/1.10/topics/python3/","canonical":"https://djangodocs.dev/ja/1.10/topics/python3/","summary":"Django 1.5 is the first version of Django to support Python 3. The same code runs both on Python 2 (≥ 2.6.5) and Python 3 (≥ 3.2), thanks to the six compatibility…","html":"<h1>Porting to Python 3<a class=\"heading-anchor\" href=\"#porting-to-python-3\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django 1.5 is the first version of Django to support Python 3. The same code\nruns both on Python 2 (≥ 2.6.5) and Python 3 (≥ 3.2), thanks to the <a class=\"reference external\" href=\"https://pythonhosted.org/six/\">six</a>\ncompatibility layer.</p>\n<p>This document is primarily targeted at authors of pluggable applications\nwho want to support both Python 2 and 3. It also describes guidelines that\napply to Django's code.</p>\n<section id=\"philosophy\">\n<h2>設計思想<a class=\"heading-anchor\" href=\"#philosophy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>This document assumes that you are familiar with the changes between Python 2\nand Python 3. If you aren't, read <a class=\"reference external\" href=\"https://docs.python.org/3/howto/pyporting.html#pyporting-howto\" title=\"(in Python v3.14)\"><span class=\"xref std std-ref\">Python's official porting guide</span></a> first. Refreshing your knowledge of unicode handling on\nPython 2 and 3 will help; the <a class=\"reference external\" href=\"http://nedbatchelder.com/text/unipain.html\">Pragmatic Unicode</a> presentation is a good\nresource.</p>\n<p>Django uses the <em>Python 2/3 Compatible Source</em> strategy. Of course, you're\nfree to chose another strategy for your own code, especially if you don't need\nto stay compatible with Python 2. But authors of pluggable applications are\nencouraged to use the same porting strategy as Django itself.</p>\n<p>Writing compatible code is much easier if you target Python ≥ 2.6. Django 1.5\nintroduces compatibility tools such as <a class=\"reference internal\" href=\"#module-django.utils.six\" title=\"django.utils.six\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.six</span></code></a>, which is a\ncustomized version of the <code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">six</span> <span class=\"pre\">module</span></code>. For convenience,\nforwards-compatible aliases were introduced in Django 1.4.2. If your\napplication takes advantage of these tools, it will require Django ≥ 1.4.2.</p>\n<p>Obviously, writing compatible source code adds some overhead, and that can\ncause frustration. Django's developers have found that attempting to write\nPython 3 code that's compatible with Python 2 is much more rewarding than the\nopposite. Not only does that make your code more future-proof, but Python 3's\nadvantages (like the saner string handling) start shining quickly. Dealing\nwith Python 2 becomes a backwards compatibility requirement, and we as\ndevelopers are used to dealing with such constraints.</p>\n<p>Porting tools provided by Django are inspired by this philosophy, and it's\nreflected throughout this guide.</p>\n</section>\n<section id=\"porting-tips\">\n<h2>Porting tips<a class=\"heading-anchor\" href=\"#porting-tips\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"unicode-literals\">\n<h3>Unicode literals<a class=\"heading-anchor\" href=\"#unicode-literals\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>This step consists in:</p>\n<ul class=\"simple\">\n<li><p>Adding <code class=\"docutils literal notranslate\"><span class=\"pre\">from</span> <span class=\"pre\">__future__</span> <span class=\"pre\">import</span> <span class=\"pre\">unicode_literals</span></code> at the top of your Python\nmodules -- it's best to put it in each and every module, otherwise you'll\nkeep checking the top of your files to see which mode is in effect;</p></li>\n<li><p>Removing the <code class=\"docutils literal notranslate\"><span class=\"pre\">u</span></code> prefix before unicode strings;</p></li>\n<li><p>Adding a <code class=\"docutils literal notranslate\"><span class=\"pre\">b</span></code> prefix before bytestrings.</p></li>\n</ul>\n<p>Performing these changes systematically guarantees backwards compatibility.</p>\n<p>However, Django applications generally don't need bytestrings, since Django\nonly exposes unicode interfaces to the programmer. Python 3 discourages using\nbytestrings, except for binary data or byte-oriented interfaces. Python 2\nmakes bytestrings and unicode strings effectively interchangeable, as long as\nthey only contain ASCII data. Take advantage of this to use unicode strings\nwherever possible and avoid the <code class=\"docutils literal notranslate\"><span class=\"pre\">b</span></code> prefixes.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p>Python 2's <code class=\"docutils literal notranslate\"><span class=\"pre\">u</span></code> prefix is a syntax error in Python 3.2 but it will be\nallowed again in Python 3.3 thanks to <span class=\"target\" id=\"index-0\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0414/\"><strong>PEP 414</strong></a>. Thus, this\ntransformation is optional if you target Python ≥ 3.3. It's still\nrecommended, per the &quot;write Python 3 code&quot; philosophy.</p>\n</aside>\n</section>\n<section id=\"string-handling\">\n<h3>String handling<a class=\"heading-anchor\" href=\"#string-handling\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Python 2's <a class=\"reference external\" href=\"https://docs.python.org/2/library/functions.html#unicode\">unicode</a> type was renamed <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#str\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">str</span></code></a> in Python 3,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">str()</span></code> was renamed <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#bytes\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">bytes</span></code></a>, and <a class=\"reference external\" href=\"https://docs.python.org/2/library/functions.html#basestring\">basestring</a> disappeared.\n<a class=\"reference external\" href=\"https://pythonhosted.org/six/\">six</a> provides <a class=\"reference internal\" href=\"#string-handling-with-six\"><span class=\"std std-ref\">tools</span></a> to deal with these\nchanges.</p>\n<p>Django also contains several string related classes and functions in the\n<a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#module-django.utils.encoding\" title=\"django.utils.encoding: A series of helper functions to manage character encoding.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.encoding</span></code></a> and <a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#module-django.utils.safestring\" title=\"django.utils.safestring: Functions and classes for working with strings that can be displayed safely without further escaping in HTML.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.safestring</span></code></a> modules. Their\nnames used the words <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>, which doesn't mean the same thing in Python 2\nand Python 3, and <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode</span></code>, which doesn't exist in Python 3. In order to\navoid ambiguity and confusion these concepts were renamed <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">text</span></code>.</p>\n<p>Here are the name changes in <a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#module-django.utils.encoding\" title=\"django.utils.encoding: A series of helper functions to manage character encoding.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.encoding</span></code></a>:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Old name</p></th>\n<th class=\"head\"><p>New name</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_bytes</span></code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_unicode</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text</span></code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">force_unicode</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">force_text</span></code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>For backwards compatibility, the old names still work on Python 2. Under\nPython 3, <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_str</span></code> is an alias for <code class=\"docutils literal notranslate\"><span class=\"pre\">smart_text</span></code>.</p>\n<p>For forwards compatibility, the new names work as of Django 1.4.2.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">注釈</p>\n<p><a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#module-django.utils.encoding\" title=\"django.utils.encoding: A series of helper functions to manage character encoding.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.encoding</span></code></a> was deeply refactored in Django 1.5 to\nprovide a more consistent API. Check its documentation for more\ninformation.</p>\n</aside>\n<p><a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#module-django.utils.safestring\" title=\"django.utils.safestring: Functions and classes for working with strings that can be displayed safely without further escaping in HTML.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.safestring</span></code></a> is mostly used via the\n<a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#django.utils.safestring.mark_safe\" title=\"django.utils.safestring.mark_safe\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mark_safe()</span></code></a> and\n<a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#django.utils.safestring.mark_for_escaping\" title=\"django.utils.safestring.mark_for_escaping\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mark_for_escaping()</span></code></a> functions, which didn't\nchange. In case you're using the internals, here are the name changes:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Old name</p></th>\n<th class=\"head\"><p>New name</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">EscapeString</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">EscapeBytes</span></code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">EscapeUnicode</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">EscapeText</span></code></p></td>\n</tr>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">SafeString</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">SafeBytes</span></code></p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">SafeUnicode</span></code></p></td>\n<td><p><code class=\"docutils literal notranslate\"><span class=\"pre\">SafeText</span></code></p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>For backwards compatibility, the old names still work on Python 2. Under\nPython 3, <code class=\"docutils literal notranslate\"><span class=\"pre\">EscapeString</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">SafeString</span></code> are aliases for <code class=\"docutils literal notranslate\"><span class=\"pre\">EscapeText</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">SafeText</span></code> respectively.</p>\n<p>For forwards compatibility, the new names work as of Django 1.4.2.</p>\n</section>\n<section id=\"str-and-unicode-methods\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">__str__()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">__unicode__()</span></code> methods<a class=\"heading-anchor\" href=\"#str-and-unicode-methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>In Python 2, the object model specifies <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> and\n` __unicode__()`_ methods. If these methods exist, they must return\n<code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> (bytes) and <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode</span></code> (text) respectively.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">print</span></code> statement and the <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#str\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">str</span></code></a> built-in call\n<a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> to determine the human-readable representation of an\nobject. The <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode</span></code> built-in calls ` __unicode__()`_ if it\nexists, and otherwise falls back to <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> and decodes the\nresult with the system encoding. Conversely, the\n<a class=\"reference internal\" href=\"/ja/1.10/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Model</span></code></a> base class automatically derives\n<a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> from ` __unicode__()`_ by encoding to UTF-8.</p>\n<p>In Python 3, there's simply <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a>, which must return <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>\n(text).</p>\n<p>(It is also possible to define <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__bytes__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__bytes__()</span></code></a>, but Django applications\nhave little use for that method, because they hardly ever deal with <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code>.)</p>\n<p>Django provides a simple way to define <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> and\n` __unicode__()`_ methods that work on Python 2 and 3: you must\ndefine a <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> method returning text and to apply the\n<a class=\"reference internal\" href=\"/ja/1.10/ref/utils/#django.utils.encoding.python_2_unicode_compatible\" title=\"django.utils.encoding.python_2_unicode_compatible\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">python_2_unicode_compatible()</span></code></a> decorator.</p>\n<p>On Python 3, the decorator is a no-op. On Python 2, it defines appropriate\n` __unicode__()`_ and <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> methods (replacing the\noriginal <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__str__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> method in the process). Here's an example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">unicode_literals</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.encoding</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">python_2_unicode_compatible</span>\n\n<span class=\"nd\">@python_2_unicode_compatible</span>\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyClass</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"s2\">&quot;Instance of my class&quot;</span>\n</code></pre></div>\n<p>This technique is the best match for Django's porting philosophy.</p>\n<p>For forwards compatibility, this decorator is available as of Django 1.4.2.</p>\n<p>Finally, note that <a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#object.__repr__\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__repr__()</span></code></a> must return a <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> on all\nversions of Python.</p>\n</section>\n<section id=\"dict-and-dict-like-classes\">\n<h3><a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">dict</span></code></a> and <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">dict</span></code></a>-like classes<a class=\"heading-anchor\" href=\"#dict-and-dict-like-classes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.keys\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.keys()</span></code></a>, <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.items\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.items()</span></code></a> and <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.values\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.values()</span></code></a> return lists in\nPython 2 and iterators in Python 3. <a class=\"reference internal\" href=\"/ja/1.10/ref/request-response/#django.http.QueryDict\" title=\"django.http.QueryDict\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code></a> and the\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">dict</span></code></a>-like classes defined in <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.datastructures</span></code>\nbehave likewise in Python 3.</p>\n<p><a class=\"reference external\" href=\"https://pythonhosted.org/six/\">six</a> provides compatibility functions to work around this change:\n<code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">iterkeys()</span></code>, <code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">iteritems()</span></code>, and <code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">itervalues()</span></code>.\nIt also contains an undocumented <code class=\"docutils literal notranslate\"><span class=\"pre\">iterlists</span></code> function that works well for\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.datastructures.MultiValueDict</span></code> and its subclasses.</p>\n</section>\n<section id=\"httprequest-and-httpresponse-objects\">\n<h3><a class=\"reference internal\" href=\"/ja/1.10/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> and <a class=\"reference internal\" href=\"/ja/1.10/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> objects<a class=\"heading-anchor\" href=\"#httprequest-and-httpresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>According to <span class=\"target\" id=\"index-1\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/\"><strong>PEP 3333</strong></a>:</p>\n<ul class=\"simple\">\n<li><p>headers are always <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> objects,</p></li>\n<li><p>input and output streams are always <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code> objects.</p></li>\n</ul>\n<p>Specifically, <a class=\"reference internal\" href=\"/ja/1.10/ref/request-response/#django.http.HttpResponse.content\" title=\"django.http.HttpResponse.content\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpResponse.content</span></code></a>\ncontains <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code>, which may become an issue if you compare it with a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> in your tests. The preferred solution is to rely on\n<a class=\"reference internal\" href=\"/ja/1.10/topics/testing/tools/#django.test.SimpleTestCase.assertContains\" title=\"django.test.SimpleTestCase.assertContains\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">assertContains()</span></code></a> and\n<a class=\"reference internal\" href=\"/ja/1.10/topics/testing/tools/#django.test.SimpleTestCase.assertNotContains\" title=\"django.test.SimpleTestCase.assertNotContains\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">assertNotContains()</span></code></a>. These methods accept a\nresponse and a unicode string as arguments.</p>\n</section>\n</section>\n<section id=\"coding-guidelines\">\n<h2>Coding guidelines<a class=\"heading-anchor\" href=\"#coding-guidelines\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The following guidelines are enforced in Django's source code. They're also\nrecommended for third-party applications that follow the same porting strategy.</p>\n<section id=\"syntax-requirements\">\n<h3>Syntax requirements<a class=\"heading-anchor\" href=\"#syntax-requirements\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"unicode\">\n<h4>ユニコード<a class=\"heading-anchor\" href=\"#unicode\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>In Python 3, all strings are considered Unicode by default. The <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode</span></code>\ntype from Python 2 is called <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> in Python 3, and <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> becomes\n<code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code>.</p>\n<p>You mustn't use the <code class=\"docutils literal notranslate\"><span class=\"pre\">u</span></code> prefix before a unicode string literal because it's\na syntax error in Python 3.2. You must prefix byte strings with <code class=\"docutils literal notranslate\"><span class=\"pre\">b</span></code>.</p>\n<p>In order to enable the same behavior in Python 2, every module must import\n<code class=\"docutils literal notranslate\"><span class=\"pre\">unicode_literals</span></code> from <code class=\"docutils literal notranslate\"><span class=\"pre\">__future__</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">__future__</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">unicode_literals</span>\n\n<span class=\"n\">my_string</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;This is an unicode literal&quot;</span>\n<span class=\"n\">my_bytestring</span> <span class=\"o\">=</span> <span class=\"sa\">b</span><span class=\"s2\">&quot;This is a bytestring&quot;</span>\n</code></pre></div>\n<p>If you need a byte string literal under Python 2 and a unicode string literal\nunder Python 3, use the <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#str\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">str</span></code></a> builtin:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"s1\">&#39;my string&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>In Python 3, there aren't any automatic conversions between <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code>, and the <a class=\"reference external\" href=\"https://docs.python.org/3/library/codecs.html#module-codecs\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">codecs</span></code></a> module became more strict. <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#str.encode\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">str.encode()</span></code></a>\nalways returns <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes.decode</span></code> always returns <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>. As a\nconsequence, the following pattern is sometimes necessary:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">value</span> <span class=\"o\">=</span> <span class=\"n\">value</span><span class=\"o\">.</span><span class=\"n\">encode</span><span class=\"p\">(</span><span class=\"s1\">&#39;ascii&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ignore&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"s1\">&#39;ascii&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Be cautious if you have to <a class=\"reference external\" href=\"https://docs.python.org/3/howto/pyporting.html#text-versus-binary-data\">index bytestrings</a>.</p>\n</section>\n<section id=\"exceptions\">\n<h4>例外<a class=\"heading-anchor\" href=\"#exceptions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>When you capture exceptions, use the <code class=\"docutils literal notranslate\"><span class=\"pre\">as</span></code> keyword:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">try</span><span class=\"p\">:</span>\n    <span class=\"o\">...</span>\n<span class=\"k\">except</span> <span class=\"n\">MyException</span> <span class=\"k\">as</span> <span class=\"n\">exc</span><span class=\"p\">:</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>This older syntax was removed in Python 3:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">try</span><span class=\"p\">:</span>\n    <span class=\"o\">...</span>\n<span class=\"k\">except</span> <span class=\"n\">MyException</span><span class=\"p\">,</span> <span class=\"n\">exc</span><span class=\"p\">:</span>    <span class=\"c1\"># Don&#39;t do that!</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>The syntax to reraise an exception with a different traceback also changed.\nUse <code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">six.reraise()</span></code>.</p>\n</section>\n</section>\n<section id=\"magic-methods\">\n<h3>Magic methods<a class=\"heading-anchor\" href=\"#magic-methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Use the patterns below to handle magic methods renamed in Python 3.</p>\n<section id=\"iterators\">\n<h4>Iterators<a class=\"heading-anchor\" href=\"#iterators\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyIterator</span><span class=\"p\">(</span><span class=\"n\">six</span><span class=\"o\">.</span><span class=\"n\">Iterator</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__iter__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span>             <span class=\"c1\"># implement some logic here</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__next__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">raise</span> <span class=\"ne\">StopIteration</span>     <span class=\"c1\"># implement some logic here</span>\n</code></pre></div>\n</section>\n<section id=\"boolean-evaluation\">\n<h4>Boolean evaluation<a class=\"heading-anchor\" href=\"#boolean-evaluation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyBoolean</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__bool__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>             <span class=\"c1\"># implement some logic here</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">__nonzero__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>      <span class=\"c1\"># Python 2 compatibility</span>\n        <span class=\"k\">return</span> <span class=\"nb\">type</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"fm\">__bool__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"division\">\n<h4>Division<a class=\"heading-anchor\" href=\"#division\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyDivisible</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__truediv__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span> <span class=\"o\">/</span> <span class=\"n\">other</span>     <span class=\"c1\"># implement some logic here</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">__div__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>   <span class=\"c1\"># Python 2 compatibility</span>\n        <span class=\"k\">return</span> <span class=\"nb\">type</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"fm\">__truediv__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__itruediv__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span> <span class=\"o\">//</span> <span class=\"n\">other</span>    <span class=\"c1\"># implement some logic here</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">__idiv__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>  <span class=\"c1\"># Python 2 compatibility</span>\n        <span class=\"k\">return</span> <span class=\"nb\">type</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"fm\">__itruediv__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Special methods are looked up on the class and not on the instance to reflect\nthe behavior of the Python interpreter.</p>\n</section>\n</section>\n<section id=\"writing-compatible-code-with-six\">\n<h3>Writing compatible code with six<a class=\"heading-anchor\" href=\"#writing-compatible-code-with-six\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"https://pythonhosted.org/six/\">six</a> is the canonical compatibility library for supporting Python 2 and 3 in\na single codebase. Read its documentation!</p>\n<p>A <a class=\"reference internal\" href=\"#module-django.utils.six\" title=\"django.utils.six\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">customized</span> <span class=\"pre\">version</span> <span class=\"pre\">of</span> <span class=\"pre\">six</span></code></a> is bundled with Django\nas of version 1.4.2. You can import it as <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.six</span></code>.</p>\n<p>Here are the most common changes required to write compatible code.</p>\n<section id=\"string-handling-with-six\">\n<span id=\"id1\"></span><h4>String handling<a class=\"heading-anchor\" href=\"#string-handling-with-six\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">basestring</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">unicode</span></code> types were removed in Python 3, and the\nmeaning of <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> changed. To test these types, use the following idioms:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">myvalue</span><span class=\"p\">,</span> <span class=\"n\">six</span><span class=\"o\">.</span><span class=\"n\">string_types</span><span class=\"p\">)</span>       <span class=\"c1\"># replacement for basestring</span>\n<span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">myvalue</span><span class=\"p\">,</span> <span class=\"n\">six</span><span class=\"o\">.</span><span class=\"n\">text_type</span><span class=\"p\">)</span>          <span class=\"c1\"># replacement for unicode</span>\n<span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">myvalue</span><span class=\"p\">,</span> <span class=\"nb\">bytes</span><span class=\"p\">)</span>                  <span class=\"c1\"># replacement for str</span>\n</code></pre></div>\n<p>Python ≥ 2.6 provides <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code> as an alias for <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>, so you don't need\n<code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">six.binary_type</span></code>.</p>\n</section>\n<section id=\"long\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">long</span></code><a class=\"heading-anchor\" href=\"#long\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">long</span></code> type no longer exists in Python 3. <code class=\"docutils literal notranslate\"><span class=\"pre\">1L</span></code> is a syntax error. Use\n<code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">six.integer_types</span></code> check if a value is an integer or a long:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">myvalue</span><span class=\"p\">,</span> <span class=\"n\">six</span><span class=\"o\">.</span><span class=\"n\">integer_types</span><span class=\"p\">)</span>      <span class=\"c1\"># replacement for (int, long)</span>\n</code></pre></div>\n</section>\n<section id=\"xrange\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">xrange</span></code><a class=\"heading-anchor\" href=\"#xrange\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If you use <code class=\"docutils literal notranslate\"><span class=\"pre\">xrange</span></code> on Python 2, import <code class=\"docutils literal notranslate\"><span class=\"pre\">six.moves.range</span></code> and use that\ninstead. You can also import <code class=\"docutils literal notranslate\"><span class=\"pre\">six.moves.xrange</span></code> (it's equivalent to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">six.moves.range</span></code>) but the first technique allows you to simply drop the\nimport when dropping support for Python 2.</p>\n</section>\n<section id=\"moved-modules\">\n<h4>Moved modules<a class=\"heading-anchor\" href=\"#moved-modules\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some modules were renamed in Python 3. The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.six.moves</span></code>\nmodule (based on the <code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">six.moves</span> <span class=\"pre\">module</span></code>) provides a\ncompatible location to import them.</p>\n</section>\n<section id=\"py2\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">PY2</span></code><a class=\"heading-anchor\" href=\"#py2\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If you need different code in Python 2 and Python 3, check <code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">six.PY2</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">if</span> <span class=\"n\">six</span><span class=\"o\">.</span><span class=\"n\">PY2</span><span class=\"p\">:</span>\n    <span class=\"c1\"># compatibility code for Python 2</span>\n</code></pre></div>\n<p>This is a last resort solution when <code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">six</span></code> doesn't provide an appropriate\nfunction.</p>\n</section>\n</section>\n<section id=\"django-customized-version-of-six\">\n<span id=\"module-django.utils.six\"></span><h3>Django customized version of <code class=\"docutils literal notranslate\"><span class=\"pre\">six</span></code><a class=\"heading-anchor\" href=\"#django-customized-version-of-six\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The version of six bundled with Django (<code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.six</span></code>) includes a few\ncustomizations for internal use only.</p>\n</section>\n</section>","rootId":"porting-to-python-3","toc":[{"title":"設計思想","anchor":"philosophy","children":[]},{"title":"Porting tips","anchor":"porting-tips","children":[{"title":"Unicode literals","anchor":"unicode-literals","children":[]},{"title":"String handling","anchor":"string-handling","children":[]},{"title":"__str__() and __unicode__() methods","anchor":"str-and-unicode-methods","children":[]},{"title":"dict and dict-like classes","anchor":"dict-and-dict-like-classes","children":[]},{"title":"HttpRequest and HttpResponse objects","anchor":"httprequest-and-httpresponse-objects","children":[]}]},{"title":"Coding guidelines","anchor":"coding-guidelines","children":[{"title":"Syntax requirements","anchor":"syntax-requirements","children":[{"title":"ユニコード","anchor":"unicode","children":[]},{"title":"例外","anchor":"exceptions","children":[]}]},{"title":"Magic methods","anchor":"magic-methods","children":[{"title":"Iterators","anchor":"iterators","children":[]},{"title":"Boolean evaluation","anchor":"boolean-evaluation","children":[]},{"title":"Division","anchor":"division","children":[]}]},{"title":"Writing compatible code with six","anchor":"writing-compatible-code-with-six","children":[{"title":"String handling","anchor":"string-handling-with-six","children":[]},{"title":"long","anchor":"long","children":[]},{"title":"xrange","anchor":"xrange","children":[]},{"title":"Moved modules","anchor":"moved-modules","children":[]},{"title":"PY2","anchor":"py2","children":[]}]},{"title":"Django customized version of six","anchor":"django-customized-version-of-six","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Django を使う","url":"/ja/1.10/topics/"}],"prev":{"docname":"topics/pagination","title":"Pagination","url":"/ja/1.10/topics/pagination/"},"next":{"docname":"topics/security","title":"Django におけるセキュリティ","url":"/ja/1.10/topics/security/"},"formats":{"html":"/ja/1.10/topics/python3/","markdown":"/ja/1.10/topics/python3.md","json":"/ja/1.10/topics/python3.json"},"source":"https://github.com/django/django/blob/stable/1.10.x/docs/topics/python3.txt","official":"https://docs.djangoproject.com/ja/1.10/topics/python3/","inVersions":["1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es","el","pl"]}