vector subscripts in Python?

Alex Martelli aleax at aleax.it
Thu Jun 12 12:58:20 EDT 2003


Alexander Schmolck wrote:
   ...
> function is worthwhile (note that it allows works for dicts and other
> iterables).
> 
> def at(iterable, indices):
>     return [iterable[i] for i in indices]

Careful: it does NOT allow for iterables -- it needs 'indexables', i.e.,
sequences or mappings.  You can't pass it a file object, a generator,
and other iterables yet.  On the other hand, it WILL be perfectly happy
with a mapping that's not iterable (i.e. doesn't implement __iter__)
as long as it does implement __getitem__ -- that's what it needs, and
all it needs.  So, I would suggest choosing a different name for the
formal argument that you have chosen to name 'iterable', as that name
might be slightly misleading.


Alex





More information about the Python-list mailing list