I have the following function:
def item_by_index(iterable, index, default=Ellipsis): """Return the item of <iterable> with the given index. Can be used even if <iterable> is not indexable. If <default> is given, it is returned if the iterator is exhausted before the given index is reached.""" iterable_slice = itertools.islice(iterable, index, None) if default is Ellipsis: return next(iterable_slice) return next(iterable_slice, default)
It should (and does) throw an exception if the
default is not given and the iterator is exhausted too soon. This is very similar to what the built-in function
Usually, one would probably use
None as default value to make an argument optional. In this case, however,
None is a valid value for
default and should be treated differently from no argument. This is why I used
Ellipsis as a default value. It sort of feels like an abuse of the
Ellipsis object, though.
Is there a better way of doing this or is
Ellipsis a good tool for the job?