obj[1] *and* obj['foo']
Jonathan Hogg
jonathan at onegoodidea.com
Wed Aug 7 03:45:19 EDT 2002
On 6/8/2002 23:11, in article ul0ich5k1nu27 at corp.supernews.com, "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?
Do you really need to subclass 'list'? i.e., do your things have to satisfy
the condition 'isinstance(mything, list)'? If not, don't subclass list and
you'll have no problems. The normal '__getitem__' function can take any
type.
The problem with subclassing list is that you end up in the dark world of
Python C-type prototols where sequences and mappings are different things.
The type subclassing magic fakes up what looks like a standard __getitem__
call, except by that point it's too late and you've been forced through the
sequence protocol and the argument must be an integer.
Just write a class like so:
>>> class Foo:
... def __getitem__( self, item ):
... print 'Item was:', item
... return 5
...
>>> f = Foo()
>>> f[7]
Item was: 7
5
>>> f['hello']
Item was: hello
5
>>>
Jonathan
More information about the Python-list
mailing list