[Python-ideas] fancy indexing

Chris Rebert pyideas at rebertia.com
Wed Jul 21 02:24:41 CEST 2010


On Tue, Jul 20, 2010 at 4:43 PM, Bruce Leban <bruce at leapyear.org> wrote:
>     x = a[[y]]
> would be approximately equivalent to
>     x = [a[i] for i in y]

You realize that syntax /already/ has a valid meaning in Python, right?
Namely, using a single-element list as a subscript:

>>> class Foo(object):
...     def __getitem__(self, index):
...         print "Subscript:", index
...
>>> a = Foo()
>>> y = 42
>>> x = a[[y]]
Subscript: [42]
>>> # hey, whaddya know!

Making this syntax do something else would lead to some surprising
inconsistencies to say the least; albeit I don't know how common it is
to use lists as subscripts.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-ideas mailing list