I'd like to know where the __setitem__
and __getitem__
code of pythons list type is defined. I know the list is implemented in C, but I can't find the code implementing the indexing and slicing operations of lists.
You'll find everything in the listobject.c
file.
It's methods are enumerated in the list_methods()
structure. You may also want to study the PyList_Type
structure for further pointers to implementations of various hooks.
Since __getitem__
and __setitem__
are part of both the C sequence protocol, the PyList_Type
structure points to a new structure list_as_sequence
that defines those hooks, listed in the tp_as_sequence
slot of PyList_Type
.
The list_item
function implements the __getitem__
hook, and __setitem__
is represented by list_ass_item
.
Slices, on the other hand, are handled by the mapping protocol, and you'll end up (via list_as_mapping
) at list_subscript
and list_ass_subscript
.