Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

slice(start, stop, None) vs slice(start, stop, 1)

I was surprised to read here that "The start and step arguments default to None", since it also says:

slice(start, stop, step=1)

"Return a slice object representing the set of indices specified by range(start, stop, step)."

So I expected the default argument value for the step parameter to be 1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know that slice(a, b, None) == slice(a, b, 1) returns False, but I am curious if slice(a, b, None) always returns the same slice as slice(a, b, 1), or if there is some example that I haven’t been able to think of for which they will return different slices.

I couldn’t find anything about this in the extensive post on slicing here

>Solution :

Slice’s step indeed defaults to None, but using step 1 and None should be equivalent for all practical purposes. That’s because in the C code where the step is actually used, there are checks which transform None into 1 anyway:

int
PySlice_GetIndices(PyObject *_r, Py_ssize_t length,
                   Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
{
    PySliceObject *r = (PySliceObject*)_r;
    if (r->step == Py_None) {
        *step = 1;
    } ...    
}

And:

int
PySlice_Unpack(PyObject *_r,
               Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
{
    PySliceObject *r = (PySliceObject*)_r;
    ...
    if (r->step == Py_None) {
        *step = 1;
    }
    ...
}

If you’re wondering why they don’t just default to 1 instead, perhaps it’s because users may still slice using None explicitly if desired e.g. L[1:2:None].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading