[Python-ideas] List indexing multiple elements

Jonathan Goble jcgoble3 at gmail.com
Mon Feb 20 16:05:57 EST 2017


On Mon, Feb 20, 2017 at 3:55 PM Ryan Gonzalez <rymg19 at gmail.com> wrote:

> - Right now, a[b,c] is already valid syntax, since it's just indexing a
> with the tuple (b, c). The proposal is to make this a specialization in the
> grammar, and also allow stuff like a[b:c, d:e] (like
> `a.__getitem__(slice(b, c), slice(d, e))`).
>

The syntax/grammar already permits slices to be used in this fashion:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Tester:
...   def __getitem__(self, *args):
...     print('object was indexed with these args:', args)
...
>>> obj = Tester()
>>> obj[3:5, 2:6, 7, -15:6:3]
object was indexed with these args: ((slice(3, 5, None), slice(2, 6, None),
7, slice(-15, 6, 3)),)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170220/7341990c/attachment.html>


More information about the Python-ideas mailing list