A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

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">&lt;<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>&gt;</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 &lt;<a href="mailto:brett@python.org">brett@python.org</a>&gt; wrote:<br>


&gt; Â Â Â  try:<br>
&gt; Â Â Â Â Â Â Â  c_heapq.heappop(Spam())<br>
&gt; Â Â Â  except TypeError:<br>
&gt; Â Â Â Â Â Â Â  # &quot;heap argument must be a list&quot;<br>
&gt; Â Â Â Â Â Â Â  pass<br>
&gt;<br>
&gt; Â Â Â  try:<br>
&gt; Â Â Â Â Â Â Â  py_heapq.heappop(Spam())<br>
&gt; Â Â Â  except AttributeError:<br>
&gt; Â Â Â Â Â Â Â  # &quot;&#39;Foo&#39; object has no attribute &#39;pop&#39;&quot;<br>
&gt; Â Â Â Â Â Â Â  pass<br>
&gt;<br>
&gt; This kind of divergence is a problem for users as they unwittingly<br>
&gt; write code that is CPython-specific. This is also an issue for other<br>
&gt; VM teams as they have to deal with bug reports from users thinking<br>
&gt; that they incorrectly implemented the module when in fact it was<br>
&gt; 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&#39;s true. I will re-word it to point that out. The example code still shows it, I just didn&#39;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>
&gt;&gt;&gt; from test.support import import_fresh_module<br>
&gt;&gt;&gt; c_heapq = import_fresh_module(&#39;heapq&#39;, fresh=[&#39;_heapq&#39;])<br>
&gt;&gt;&gt; py_heapq = import_fresh_module(&#39;heapq&#39;, blocked=[&#39;_heapq&#39;])<br>
</div>&gt;&gt;&gt; from collections import UserList<br>
&gt;&gt;&gt; class Seq(UserList): pass<br>
...<br>
&gt;&gt;&gt; c_heapq.heappop(UserList())<br>
Traceback (most recent call last):<br>
 Â File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>
<div class="im">TypeError: heap argument must be a list<br>
</div>&gt;&gt;&gt; py_heapq.heappop(UserList())<br>
Traceback (most recent call last):<br>
 Â File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>
 Â File &quot;/home/ncoghlan/devel/py3k/Lib/heapq.py&quot;, line 140, in heappop<br>
 Â  Â lastelt = heap.pop() Â  Â # raises appropriate IndexError if heap is empty<br>
 Â File &quot;/home/ncoghlan/devel/py3k/Lib/collections/__init__.py&quot;, 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>
&gt;&gt;&gt; 1()<br>
Traceback (most recent call last):<br>
 Â File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>
TypeError: &#39;int&#39; object is not callable<br>
&gt;&gt;&gt; with 1: pass<br>
...<br>
Traceback (most recent call last):<br>
 Â File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>
AttributeError: &#39;int&#39; object has no attribute &#39;__exit__&#39;<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