Extended slice in python?

python, slice

Solution

`__setslice__` is deprecated - you'll want to use `__setitem__` with a slice argument.

Note that, per the Python documentation, slices can only be done in the following syntactic forms: `a[i:j:step]`, `a[i:j, k:l]`, or `a[..., i:j]`. The colon-based syntax is used to define a single slice object, but as the second example shows you can have multiple slice arguments (they get passed in as a tuple of slices).

Here's an example which prints the `key` argument, to see its form:

>>> class Foo(object):
>>>     def __setitem__(self, key, value):
>>>         print key
>>> 
>>> a = Foo()
>>> a[1:1] = 1
slice(1, 1, None)
>>> 
>>> a[1:1:1] = 1
slice(1, 1, 1)
>>> 
>>> a[1:1, 1:1] = 1
(slice(1, 1, None), slice(1, 1, None))

Problem

I am trying to write a BitArray class, and it would be nifty to have something like numpy's array, `x[i:j:k] = val`. How would I write this in Python? Not with the `__setslice__`, right? Because that only takes three arguments and I need one to take four.

Original source