Suggested feature: slice syntax within tuples (or even more generally)?

stephenwlin at gmail.com stephenwlin at gmail.com
Wed Feb 13 20:00:15 CET 2013


Hello,

Would it be feasible to modify the Python grammar to allow ':' to generate slice objects everywhere rather than just indexers and top-level tuples of indexers?

Right now in Py2.7, Py3.3:
    "obj[:,2]" yields "obj[slice(None),2]"
but
    "obj[(:,1),2]" is an error, instead of "obj[(slice(None), 1), 2]"

Also, more generally, you could imagine this working in (almost?) any expression without ambiguity:
    "a = (1:2)" could yield "a = slice(1,2)"

See motivating discussion for this at:
    https://github.com/pydata/pandas/issues/2866

There might not be very many use cases for this currently outside of pandas, but apparently the grammar was already modified to allow '...' outside indexers and there's probably even fewer valid use cases for that:
    "e = ..." yields "e = Ellipsis"

Would there be any downside to changing the handling of ':' as well? It might even make the grammar simpler, in some ways, since indexers won't have to be treated specially.

Let me know if you have any thoughts.

Thanks!
Stephen



More information about the Python-list mailing list