Trouble when overloading operator[ ]

Stephen Hansen stephen at cerebralmaelstrom.com
Thu Jul 20 10:52:51 EDT 2000


    You can't quite do it that way, I don't think :)

    In Python, the my_class[2][3] is evaluated first as my_class[2],
and whatever value is returned there is what the [3] is playing on.

For instance:

sublist0 = [1,2,3,4]
sublist1 = [5,6,7,8]
sublist2 = [9,10,11,12]

biglist = [sublist1, sublist2, sublist3]

>>> biglist[2][3]
12

'my_class[2][3]' implies a sequence of sequences -- the second sequence
has its /own/ __getitem__ that has to be called. So, you can either have
my_class's __getitem__ return another class with its own getitem, or you
can use slices... my_class[2:3]?

--S

<manolop at my-deja.com> wrote in message news:8l720o$lr4$1 at nnrp1.deja.com...
> Hi!
>
> I need to overload the [ ] operator in a class, so that I can do things
> like my_class[2][3]
>
> Everything works fine when I only use one [ ] and the correct values
> are passed to __getitem__, but I run into trouble when I add the second
> [ ].  Should I use __getitem__ for the double[ ] or is there another
> method? O:-)
>
> TIA
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list