I want to know which method is called by the [] operator. I know it is __getitem__, but I wonder how one could figure this out programmatically (without looking it up in some docs). I imagine something like the below, but somehow easier:
from unittest import mock
class MyList(list): # needed because list attributes are read-only
pass
names = {}
for attrname in list.__dict__.keys():
try:
with mock.patch(f"__main__.MyList.{attrname}"):
l = MyList([1])
_ = l[0]
call_count = l.__getattribute__(attrname).call_count
if call_count > 0:
names[attrname] = call_count
except Exception as e:
pass
print(names)