<div class="gmail_quote">On Tue, May 31, 2011 at 3:46 PM, Eric Snow <span dir="ltr"><<a href="mailto:ericsnowcurrently@gmail.com">ericsnowcurrently@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Looking at the ABC code [1], I noticed that Mapping's __eq__ method can return NotImplemented.  This got me curious as to why you would return NotImplemented and not raise a TypeError or a NotImplementedError.  <div><br>

</div><div>There was an issue regarding this last year [2] that indicated the reason for Mapping's behavior.  Also, I found a thread from python-dev [3] which talks about the use of NotImplemented (the thread relates to numeric coercion).  I found some older documentation on informal coercion guidelines that helped [4].  My understanding is that NotImplemented is used for binary operators: the right hand side of the operator should be tried if the left hand side returns NotImplemented.  So it's more like DoNotKnowHowToHandleArgumentOfThatType.  I think the name may have added to my confusion.   (BTW, I learned that NotImplemented is a singleton, like None).<div>

<div><br></div><div>Is binary operators the only place that NotImplemented is used under the hood?  Is it all binary operators, and if not, where does it say which operators use NotImplemented?  This would have bearing on when I would need to return it.</div>

<div><br></div><div>In the python-dev thread [3], MRAB indicates NotImplemented is used instead of exceptions for performance reasons.  Is raising NotImplementedError or TypeError that big a difference?  I expect if you are using the operator on a large loop it could matter, but how much?</div>

<div><br></div><div>Guido indicates earlier in the thread that NotImplemented is used so that you know that it came from the function that you directly called, and not from another call inside that function.  Why does it matter if it came directly from the function or not?  And couldn't the NotImplemented still have come from a call inside the operator, rather than directly?</div>

<div><br></div><div>As an aside, don't we have that same situation all over the place.  For instance, using __getattribute__, how do you know if an AttributeError means that the attribute was not found on the object?  It could mean that __getattribute__ called something that raised the exception (and perhaps it should have handled it).  Does it matter?  Is there a good way to tell the difference, or would it be good practice to always handle explicitly in a function any exception type that you may be raising there?</div>

<div><div><br></div><div>Thanks,</div><div><br></div><div>-eric</div><div><br></div><div>[1] <a href="http://hg.python.org/cpython/file/29e08a98281d/Lib/collections/abc.py#l398" target="_blank">http://hg.python.org/cpython/file/29e08a98281d/Lib/collections/abc.py#l398</a></div>

<div>[2] <a href="http://bugs.python.org/issue8729" target="_blank">http://bugs.python.org/issue8729</a></div><div>[3] <a href="http://mail.python.org/pipermail/python-dev/2005-March/051835.html" target="_blank">http://mail.python.org/pipermail/python-dev/2005-March/051835.html</a></div>

</div></div><div>[4] <a href="http://docs.python.org/release/2.5.2/ref/coercion-rules.html" target="_blank">http://docs.python.org/release/2.5.2/ref/coercion-rules.html</a></div></div>
</blockquote></div><div><br></div>I'm guessing that <a href="http://docs.python.org/reference/datamodel.html">http://docs.python.org/reference/datamodel.html</a> is the only place in the docs that talks about the use of NotImplemented.<div>
<br></div><div>-eric</div>