[Python-Dev] itertools addition: getitem()

Guido van Rossum guido at python.org
Sun Jul 8 13:31:07 CEST 2007

How important is it to have the default in this API? __getitem__()
doesn't have a default; instead, there's a separate API get() that
provides a default (and I find defaulting to None more manageable than
the "_default = object()" pattern).


On 7/8/07, Walter Dörwald <walter at livinglogic.de> wrote:
> I'd like to propose the following addition to itertools: A function
> 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 (this requires additional temporary storage for abs(index)
> objects).
> The patch is available at http://bugs.python.org/1749857
> Servus,
>     Walter
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list