what does it mean if an object is subscriptable or not?


Question

Which types of objects fall into the domain of "subscriptable"?

1
301
7/17/2019 7:05:38 AM

Accepted Answer

It basically means that the object implements the __getitem__() method. In other words, it describes objects that are "containers", meaning they contain other objects. This includes lists, tuples, and dictionaries.

276
10/19/2008 10:35:05 PM

Off the top of my head, the following are the only built-ins that are subscriptable:

string:  "foobar"[3] == "b"
tuple:   (1,2,3,4)[3] == 4
list:    [1,2,3,4][3] == 4
dict:    {"a":1, "b":2, "c":3}["c"] == 3

But mipadi's answer is correct - any class that implements __getitem__ is subscriptable


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon