<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Greetings,</div><div><br></div><div>This is a request to fix the documentation for __instancecheck__.</div><div><br></div><div>Please add the following (please rewrite better than I can -- I am not good at explaining concepts in short sentences):<br></div><div><br></div><div>NOTE: As an optimization,
<code class="gmail-descname">isinstance</code><span class="gmail-sig-paren">(</span><em>object</em>, <em>classinfo</em><span class="gmail-sig-paren">)</span> does NOT call classinfo.__instancecheck__(instance) when type(object) == classinfo.</div><div><br></div><div>Consider the following program:</div><div><br></div><div>class M(type):<br> def __instancecheck__(m, t):<br> print('instancecheck(%s, %s)' % (m, t))<br> return False # LIE!<br><br>Test = M('Test', ((object,)), {})<br><br>something = Test()<br><br>print('Does *NOT* call __instancecheck__:')<br>print('isinstance(something, Test): %s' % isinstance(something, Test))<br><br>print()<br>print('Does call __instancecheck__:')<br>print('isinstance(0, Test): %s' % isinstance(0, Test))<br></div><div><br></div><div>Under python 2, python 3, and pypy, in all cases, the first examples does *NOT* call __instancecheck__.<br></div><div><br></div><div>You can see the optimization here:</div><div><br></div><div><a href="https://github.com/python/cpython/blob/master/Objects/abstract.c#L2397-L2405">https://github.com/python/cpython/blob/master/Objects/abstract.c#L2397-L2405</a></div><div><br></div><div>Which reads:</div><div><br></div><div>
<table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2397" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted"><span class="gmail-pl-k">int</span></td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2398" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted"><span class="gmail-pl-en">PyObject_IsInstance</span>(PyObject *inst, PyObject *cls)</td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2399" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted">{</td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2400" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted"> <span class="gmail-pl-c1">_Py_IDENTIFIER</span>(__instancecheck__);</td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2401" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted"> PyObject *checker;</td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2402" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted">
<br></td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2403" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted"> <span class="gmail-pl-c"><span class="gmail-pl-c">/*</span> Quick test for an exact match <span class="gmail-pl-c">*/</span></span></td>
</tr>
<tr>
</tr></tbody></table><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container"><tbody><tr><td id="gmail-LC2404" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line gmail-highlighted"> <span class="gmail-pl-k">if</span> (<span class="gmail-pl-c1">Py_TYPE</span>(inst) == (PyTypeObject *)cls)</td>
</tr>
<tr>
</tr></tbody></table> <span class="gmail-pl-k">return</span> <span class="gmail-pl-c1">1</span>;
<br><br></div><div>I'm fine with the optimization -- I am not suggesting to get rid of it.</div><div><br></div><div>I just want the documentation to match the actual implementation.</div><div><br></div><div>The following documentation needs to be fixed:</div><div><br></div><div><a href="https://docs.python.org/2/reference/datamodel.html">https://docs.python.org/2/reference/datamodel.html</a></div><div><a href="https://docs.python.org/3/reference/datamodel.html">https://docs.python.org/3/reference/datamodel.html</a></div><div><a href="https://www.python.org/dev/peps/pep-3119/">https://www.python.org/dev/peps/pep-3119/</a></div><div><br></div><div>It took me half an hour to figure out why my version of __instancecheck__ was not working, as I tried to test it using the super simple code above ...</div><div><br></div><div>One of the best things about python is how accurate and consistent the documentation is.</div><div><br></div><div>This request is to keep these high standards.</div><div><br></div><div>Thanks,</div><div><br></div><div>Joy Diamond.</div><div><br></div><div>NOTE: I'm not sure where to post this, so posting to python-ideas, in case people want to discuss getting rid of the optimization in
<span class="gmail-pl-en">PyObject_IsInstance</span> ... which I am not suggesting.<br></div><div><br></div><div><br></div><div><br></div></div></div></div></div></div></div></div>