overloading indexing

Pete Shinners pete at visionart.com
Tue Sep 4 18:57:29 EDT 2001


Janos Blazi wrote:

> One can overload a[x] (when a is a class an one uses __getitem__) but it is
> not possible to overload double indexing, i.e. a[x,y]. Am I right? And
> a[x,y] is not the same as a[x][y] (as the latter can be overloaded).

it can be done, when the user passes more than one index
they will be combined into a tuple for you. also, there's
no need to just use integers :]

 >>> class K:
...     def __getitem__(self, args):
...             print 'GETITEM ARGS:', args
...
 >>> k=K()
 >>> k[1]
GETITEM ARGS: 1
 >>> k[1,2]
GETITEM ARGS: (1, 2)
 >>> k['bread', 'milk']
GETITEM ARGS: ('bread', 'milk')




More information about the Python-list mailing list