Showing content from http://mail.python.org/pipermail/python-dev/attachments/20110406/79246962/attachment-0001.html below:
<br><br><div class="gmail_quote">On Tue, Apr 5, 2011 at 05:01, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Tue, Apr 5, 2011 at 9:46 AM, Brett Cannon <<a href="mailto:brett@python.org">brett@python.org</a>> wrote:<br>
> Â Â Â try:<br>
> Â Â Â Â Â Â Â c_heapq.heappop(Spam())<br>
> Â Â Â except TypeError:<br>
> Â Â Â Â Â Â Â # "heap argument must be a list"<br>
> Â Â Â Â Â Â Â pass<br>
><br>
> Â Â Â try:<br>
> Â Â Â Â Â Â Â py_heapq.heappop(Spam())<br>
> Â Â Â except AttributeError:<br>
> Â Â Â Â Â Â Â # "'Foo' object has no attribute 'pop'"<br>
> Â Â Â Â Â Â Â pass<br>
><br>
> This kind of divergence is a problem for users as they unwittingly<br>
> write code that is CPython-specific. This is also an issue for other<br>
> VM teams as they have to deal with bug reports from users thinking<br>
> that they incorrectly implemented the module when in fact it was<br>
> caused by an untested case.<br>
<br>
</div>While I agree with the PEP in principle, I disagree with the way this<br>
example is written. Guido has stated in the past that code simply<br>
*cannot* rely on TypeError being consistently thrown instead of<br>
AttributeError (or vice-versa) when it comes to duck-typing. Code that<br>
cares which of the two is thrown is wrong.<br>
<br>
However, there actually *is* a significant semantic discrepancy in the<br>
heapq case, which is that py_heapq is duck-typed, while c_heapq is<br>
<div class="im">not:<br></div></blockquote><div><br>That's true. I will re-word it to point that out. The example code still shows it, I just didn't explicitly state that in the example.<br><br>-Brett<br>Â </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">
<br>
>>> from test.support import import_fresh_module<br>
>>> c_heapq = import_fresh_module('heapq', fresh=['_heapq'])<br>
>>> py_heapq = import_fresh_module('heapq', blocked=['_heapq'])<br>
</div>>>> from collections import UserList<br>
>>> class Seq(UserList): pass<br>
...<br>
>>> c_heapq.heappop(UserList())<br>
Traceback (most recent call last):<br>
 File "<stdin>", line 1, in <module><br>
<div class="im">TypeError: heap argument must be a list<br>
</div>>>> py_heapq.heappop(UserList())<br>
Traceback (most recent call last):<br>
 File "<stdin>", line 1, in <module><br>
 File "/home/ncoghlan/devel/py3k/Lib/heapq.py", line 140, in heappop<br>
  lastelt = heap.pop()   # raises appropriate IndexError if heap is empty<br>
 File "/home/ncoghlan/devel/py3k/Lib/collections/__init__.py", line 848, in pop<br>
  def pop(self, i=-1): return self.data.pop(i)<br>
IndexError: pop from empty list<br>
<br>
Cheers,<br>
Nick.<br>
<br>
P.S. The reason I was bugging Guido to answer the TypeError vs<br>
AttributeError question in the first place was to find out whether or<br>
not I needed to get rid of the following gross inconsistency in the<br>
behaviour of the with statement relative to other language constructs:<br>
<br>
>>> 1()<br>
Traceback (most recent call last):<br>
 File "<stdin>", line 1, in <module><br>
TypeError: 'int' object is not callable<br>
>>> with 1: pass<br>
...<br>
Traceback (most recent call last):<br>
 File "<stdin>", line 1, in <module><br>
AttributeError: 'int' object has no attribute '__exit__'<br>
<br>
<br>
<br>
Cheers,<br>
Nick.<br>
<font color="#888888"><br>
--<br>
Nick Coghlan  |  <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>  |  Brisbane, Australia<br>
</font></blockquote></div><br>
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4