[Python-ideas] Range and slice syntax

Steven D'Aprano steve at pearwood.info
Mon Nov 12 16:10:06 EST 2018


On Mon, Nov 12, 2018 at 11:23:21AM -0500, David Mertz wrote:

> >>> import pandas as pd
> >>> import numpy as np
> >>> I = pd.IndexSlice
> >>> J = np.s_
> >>> I[4:10:3]
> slice(4, 10, 3)

I'm not entirely sure that I like the idea of conflating slice 
*constructor* with slice *usage*. Slice objects are objects, like any 
other, and I'm not convinced that overloading slice syntax to create 
slice objects is a good design.

I'm pretty sure it would have confused the hell out of me as a beginner 
to learn that mylist[1::2] took a slice and [1::2] make a slice.

But at least numpy and pandas has the virtual of needing a prefix to 
make it work.


> You don't get your range-like behavior with those, but it's easy to
> construct. I'm having a think-o. I think it should be possible to make a
> RangeSlice class that will act like an enhanced version of pd.IndexSlice,
> but my try was wrong.

Just because we can do something, doesn't mean we should.


-- 
Steve


More information about the Python-ideas mailing list