[Python-Dev] Why does __getitem__ slot of builtin call sequence methods first?

Guido van Rossum guido at python.org
Sun Oct 2 02:41:32 CEST 2005

On 10/1/05, Travis Oliphant <oliphant at ee.byu.edu> wrote:
> The new ndarray object of scipy core (successor to Numeric Python) is a
> C extension type that has a getitem defined in both the as_mapping and
> the as_sequence structure.
> The as_sequence mapping is just so PySequence_GetItem will work correctly.
> As exposed to Python the ndarray object has a .__getitem__  wrapper method.
> Why does this wrapper call the sequence getitem instead of the mapping
> getitem method?
> Is there anyway to get at a mapping-style __getitem__ method from Python?

Hmm... I'm sure the answer is in typeobject.c, but that is one of the
more obfuscated parts of Python's guts. I wrote it four years ago and
since then I've apparently lost enough brain cells (or migrated them
from language implementation to to language design service :) that I
don't understand it inside out any more like I did while I was in the
midst of it.

However, I wonder if the logic isn't such that if you define both
sq_item and mp_subscript, __getitem__ calls sq_item; I wonder if by
removing sq_item it might call mp_subscript? Worth a try, anyway.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list