From: justin w. <ju...@tr...> - 2003-02-27 19:31:03
|
Hi, Trying to create a custom array structure. Ideally I would like to subclass the built in list, but it seems this feature won't be available till version 2.2 (as per CPython) so instead I am extending java.util.ArrayList as per below. This works fine, and I can access the object using standard array syntax (x[0] .. etc). However when I look under the hood / overload some of the built in methods, it appears they are different from CPython. In particular, __repr__ gets called as expected but __getitem__ does not (see below). (If I run the code in CPython 2,2, replacing ArrayList with list, it works as expected). Perhaps someone can shed some light on what is happening here. thanks! Justin from java.util import * class test(ArrayList): def __getitem__(self,i): print "__getitem__"+str(i) return ArrayList.get(self,i) def __repr__(self): print "__repr__" r="" for i in self: r=r+str(i)+chr(10) return r def main(): r=test() r.add("one") r.add("two") print r # successfully calls __repr__ print r[0] # returns the right result, but doens't call __getitem__ main() |