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.
I’d like to know where the __setitem__ and __getitem__ code of pythons list type
Share
You’ll find everything in the
listobject.cfile.It’s methods are enumerated in the
list_methods()structure. You may also want to study thePyList_Typestructure for further pointers to implementations of various hooks.Since
__getitem__and__setitem__are part of both the C sequence protocol, thePyList_Typestructure points to a new structurelist_as_sequencethat defines those hooks, listed in thetp_as_sequenceslot ofPyList_Type.The
list_itemfunction implements the__getitem__hook, and__setitem__is represented bylist_ass_item.Slices, on the other hand, are handled by the mapping protocol, and you’ll end up (via
list_as_mapping) atlist_subscriptandlist_ass_subscript.