[Python-ideas] PEP pre-draft: Support for indexing with keyword arguments

Nathaniel Smith njs at pobox.com
Fri Jul 4 22:39:00 CEST 2014


On Fri, Jul 4, 2014 at 9:10 PM, Tim Delaney <timothy.c.delaney at gmail.com> wrote:
> 1. I think you absolutely *must* address the option of purely syntactic
> sugar in the PEP. It will come up on python-dev, so address it now.
>
>     a[b, c=f, e=f:g:h]
>     -> a[b, 'c':d, 'e':slice(f, g, h)]
>
> The rationale is readability and being both backwards and forwards
> compatible - existing __getitem__ designed to abuse slices will continue to
> work, and __getitem__ designed to work with the new syntax will work by
> abusing slices in older versions of Python.

I don't know of any existing code that abuses slices in this way (so
worrying about compatibility with it seems odd?).

> Pandas could be cited as an example of an existing library that could
> potentially benefit. It would be good if there were precise examples of
> Pandas syntax that would benefit immediately, but I don't know it beyond a
> cursory glance over the docs. My gut feeling from that is that if the syntax
> were available Pandas might be able to use it effectively.

Your hack (aside from being pointlessly ugly) would actually prevent
pandas from using this feature. In pandas, slices like foo["a":"b"]
already have a meaning (i.e., take all items from the one labeled "a"
to the one labeled "b").

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org


More information about the Python-ideas mailing list