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> <<a href="mailto:guido@python.org">guido@python.org</a>> 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> compid = root[ns.company_id].next()<br>except StopIteration:<br> compid = None
<br>else:<br> compid = int(compid)<br><br>While this is more lines, it doesn't require one to know about<br>getitem() on an iterator. This is the same reason why setdefault() was<br>a mistake -- it'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'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;"> '''<br> Return the n'th next element for seq, if it exists.
<br><br> If default is specified, it is return when the sequence is too short.<br> Otherwise StopIteration is raised.<br> '''<br> try:<br> for i in xrange(n-1):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> seq.next()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return seq.next()<br> except StopIteration:<br>
if default is _nothing:<br> raise<br> return default<br></span><br>The nice thing about this function is that it solves several problems in one: extraction of the n'th next element, testing for a minimum sequence length given a sentinel value, and just skipping n elements. It also leaves the sequence in a useful and predictable state, which is not true of the Python-version getitem code. While cute, I can'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;"> 'Return the length of the hopefully finite sequence'
<br> n = 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for x in seq:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
n += 1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return n</span><br style="font-family: courier new,monospace;"><br>Why? Because I find myself implementing it in virtually every project. Maybe I'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