Negative array indicies and slice()

Chris Rebert clp2 at rebertia.com
Mon Oct 29 04:37:47 EDT 2012


On Mon, Oct 29, 2012 at 1:24 AM,  <andrewr3mail at gmail.com> wrote:
> On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
>> On Oct 29, 2:09 pm, Andrew <andrewr3m... at gmail.com> wrote:
<snip>
>>     class RangedSlicer(list):
<snip>
>> Then wrap your lists with your RangedSlicer class as needed.
>
> Hmmm...
>
> I began a test in an interactive shell:
>>>> class RangedSlicer(list):
> ...     def __getitem__(self,item):
> ...             print item
>
This just defines a class; it doesn't modify in-place the normal
behavior of plain lists. You have to actually *use* the class.

>>>> a=[1,2,3,4,5]

You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer!
You wanted:
a = RangedSlicer([1,2,3,4,5])

>>>> a.__getitem__( slice(1,5) )
> [2, 3, 4, 5]
>
> Very odd...  I would have expected [1,2,3,4]

"[2, 3, 4, 5]" is the return value from `a.__getitem__( slice(1,5) )`
(or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of
"print item"; that line of code is never executed since you never used
the RangedSlicer class at all.

Regards,
Chris



More information about the Python-list mailing list