What does it mean if a Python object is "subscriptable" or not? What does it mean if a Python object is "subscriptable" or not? python python

What does it mean if a Python object is "subscriptable" or not?


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 strings, lists, tuples, and dictionaries.


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] == 4list:    [1,2,3,4][3] == 4dict:    {"a":1, "b":2, "c":3}["c"] == 3

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


A scriptable object is an object that records the operations done to it and it can store them as a "script" which can be replayed.

For example, see: Application Scripting Framework

Now, if Alistair didn't know what he asked and really meant "subscriptable" objects (as edited by others), then (as mipadi also answered) this is the correct one:

A subscriptable object is any object that implements the __getitem__ special method (think lists, dictionaries).