slice objects vs. []

Quinn Dunkan quinn at regurgitate.ugcs.caltech.edu
Wed May 22 18:58:52 EDT 2002


On 23 May 2002 00:20:53 +0200, Chris Liechti <cliechti at gmx.net> wrote:
>that __getitem__ does not work with slices, aplies only built-in types and 
>subclasses of it. i think this is rooted in the C implementation of those 
>object (the two methods are still separate there) that might change when 
>the type/class unification gets further or in Python 3.0.

I see no reason why it can't be fixed sooner than that.  I'll try the patch
route :)

>however it works perfectly for user defines classes, as described in the 
>docs:
>
>>>> class A:
>... 	def __getitem__(self, item):
>... 		print item
>... 		
>>>> a=A()
>>>> a[1:2]
>slice(1, 2, None)

Well, "works perfectly" is an easy goal to achieve if you define it to
"prints something on the screen" <wink>

I'd guess that a large proportion of user defined __getitem__s use the built in
list somewhere down the line, and all of those that do not contain code to
explicitly support slices will not work with them, even though they could.
Unless they try arithmetic or something on the index.



More information about the Python-list mailing list