Is it possible to define a class with a __getitem__ that takes a tuple argument using the Python C-API?
The sq_item member of the tp_as_sequence member of a PyTypeObject
must be a ssizeargfunc, so I don’t see how to do it.
(But I assume that the NumPy ndarray does it.)
Is it possible to define a class with a __getitem__ that takes a tuple
Share
Yes, use
tp_as_mappinginstead.Its
mp_subscripttakes aPyObject *so you can use anything as index/key.To understand how they relate, you could have a look at the source of
PyObject_GetItem()which (as the doc says) is the equivalent of Pythono[key]expression. You will see that it first triestp_as_mappingand if that’s not there and key isint, it triestp_as_sequence.