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/20070708/af8ba064/attachment.htm below:

<div><span class="gmail_quote">On 7/8/07, <b class="gmail_sendername">Guido van Rossum</b> &lt;<a href="mailto:guido@python.org">guido@python.org</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Ahem. I hope you have a better use case for getitem() than that<br>(regardless of the default issue). I find it clearer to write that as<br><br>try:<br>&nbsp;&nbsp;compid = root[ns.company_id].next()<br>except StopIteration:<br>&nbsp;&nbsp;compid = None
<br>else:<br>&nbsp;&nbsp;compid = int(compid)<br><br>While this is more lines, it doesn&#39;t require one to know about<br>getitem() on an iterator. This is the same reason why setdefault() was<br>a mistake -- it&#39;s too obscure to invent a compact spelling for it
<br>since the compact spelling has to be learned or looked up.<br></blockquote></div><br><br>Apropos of this discussion, I&#39;ve occasionally wanted a faster version of the following:<br><br><span style="font-family: courier new,monospace;">
_nothing=object()<br><br>def nth_next(seq,n,default=_nothing):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; &#39;&#39;&#39;<br>&nbsp; Return the n&#39;th next element for seq, if it exists.
<br><br>&nbsp; If default is specified, it is return when the sequence is too short.<br>&nbsp; Otherwise StopIteration is raised.<br>&nbsp; &#39;&#39;&#39;<br>&nbsp; try:<br>&nbsp; &nbsp; for i in xrange(n-1):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; &nbsp; &nbsp; seq.next()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; &nbsp; return seq.next()<br>&nbsp; except StopIteration:<br>
&nbsp;&nbsp;&nbsp; if default is _nothing:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise<br>&nbsp;&nbsp;&nbsp; return default<br></span><br>The nice thing about this function is that it solves several problems in one: extraction of the n&#39;th next element, testing for a minimum sequence length given a sentinel value, and just skipping n elements.&nbsp; It also leaves the sequence in a useful and predictable state, which is not true of the Python-version getitem code.&nbsp; While cute, I can&#39;t say if it is worthy of being an itertool function.
<br><br>Also vaguely apropos:<br><br><span style="font-family: courier new,monospace;">def ilen(seq):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; &#39;Return the length of the hopefully finite sequence&#39;
<br>&nbsp; n = 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; for x in seq:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
&nbsp; &nbsp; n += 1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; return n</span><br style="font-family: courier new,monospace;"><br>Why?&nbsp; Because I find myself implementing it in virtually every project.&nbsp; Maybe I&#39;m just an outlier, but many algorithms I implement need to consume iterators (for side-effects, obviously) and it is sometimes nice to know exactly how many elements were consumed.
<br><br>~Kevin<br><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