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

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Aug 7 13:18:01 EDT 2002


Travis Shirk <travis at pobox.com> wrote:
>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?

search google for seqdict.

Huaiyu



More information about the Python-list mailing list