__getitem__

I am reviewing some code that I would like to use, but in one of the classes, there is a ‘convenience method’ as follows
def __getitem__(self, keyname) and I am having trouble trying to interpret this. I have looked up the Python documentation but don’t understand getitem. Can someone explain it in layman’s terms?

The right way to use __getitem__ is to get Python to call it for you.
http://www.diveintopython.net/object_oriented_framework/special_class_methods.html

That’s why __getitem__ is a special class method; not only can you call it yourself, you can get Python to call it for you by using the right syntax.

1 Like