obj[1] *and* obj['foo']

Travis Shirk travis at pobox.com
Wed Aug 7 00:11:29 CEST 2002


I'm trying to write a class that implements __getitem__(self, k),
but I'd like to invoke the method with integer keys and string keys.
Currently, I'm getting exceptions when I use string keys:

TypeError: sequence index must be integer

I'm subclassing list (python 2.2) so I understand why the *sequence* does
not allow non-int keys, and I assume that if I subclassed dict the reverse
type clash would occur.  

So, is there a way to "override" __getitem__ to accept both types of
access?

Thanks,
Travis

-- 
-- 
Travis Shirk <travis at pobox dot com>



More information about the Python-list mailing list