
pete at visionart
Sep 4, 2001, 3:57 PM
Post #2 of 2
(329 views)
Permalink
|
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')
|