[Patches] [ python-Patches-1749857 ] itertools.getitem()
SourceForge.net
noreply at sourceforge.net
Tue Jul 31 09:03:40 CEST 2007
Patches item #1749857, was opened at 2007-07-08 04:33
Message generated for change (Comment added) made by rhettinger
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1749857&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Modules
Group: Python 2.6
>Status: Closed
>Resolution: Rejected
Priority: 5
Private: No
Submitted By: Walter Dörwald (doerwalter)
Assigned to: Raymond Hettinger (rhettinger)
Summary: itertools.getitem()
Initial Comment:
This patch adds itertools.getitem() which is basically equivalent to the following python code:
_default = object()
def getitem(iterable, index, default=_default):
try:
return list(iterable)[index]
except IndexError:
if default is _default:
raise
return default
but without materializing the complete list. Negative indexes are supported too.
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2007-07-31 02:03
Message:
Logged In: YES
user_id=80475
Originator: NO
Rejected after an offline discussion with the OP.
The use case for getitem(n) with n as a negative number depended on an
unlikely combination of circumstances:
* you have an iterable that is not a sequence (otherwise, just use
s[-n]).
* the iterable is somewhat large (otherwise, just list it into memory)
* the iterable is finite (otherwise, getitem() dies in a infinite loop)
* you only want one entry (otherwise, you'll have make multiple passes)
* that entry is located near the end (otherwise getitem() is memory
intensive)
* you know the entry's offset from the end but not from the beginning
* identifying the record of interest depends only on its position, not its
content
In the common case where the index is zero, the preferred spelling is to
use the next() method -- that is its purpose. For cases where the index is
a positive integer, uing islice(it, n).next() suffices though it doesn't
have the cute feature for a default value.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1749857&group_id=5470
More information about the Patches
mailing list