Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

overloading indexing

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jblazi at hotmail

Sep 4, 2001, 1:54 PM

Post #1 of 2 (330 views)
Permalink
overloading indexing

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).


Janos Blazi




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
Check out our new Unlimited Server. No Download or Time Limits!
-----== Over 80,000 Newsgroups - 19 Different Servers! ==-----


pete at visionart

Sep 4, 2001, 3:57 PM

Post #2 of 2 (329 views)
Permalink
overloading indexing [In reply to]

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')

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.