Slices when extending python with C++

Robert Kern robert.kern at gmail.com
Wed Dec 28 04:24:42 EST 2011


On 12/27/11 11:02 PM, rozelak at volny.cz wrote:
> Hallo,
> I have kind of special question when extening python with C++
> implemented modules.
>
> I try to implement a class, behaving also like an array. And I need
> to implement slice-getters. I implemented PySequenceMethods.sq_slice
> to get "simple" slices like:
>
> myobj[x:y]
>
> It works perfectly, without problems. But now I have a problem when
> need to access the array with "special" slices, like:
>
> myobj[x:y:step]
> myobj[::-1]  # to revert the array
>
> I would like to ask which method must be implemented to get this
> "special" slice getters. The "simple" slice
> PySequenceMethods.sq_slice getter accepts only two indexes - from,
> to, but not a step (which would solve the issue).

The sq_slice slot is deprecated, just like the __getslice__() method on the 
Python side. Instead, implement sq_item to accept a slice object in addition to 
integer indices.

http://docs.python.org/c-api/slice.html

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list