How to determine an object is "scriptable"

Peter Hansen peter at engcorp.com
Thu Mar 30 18:30:37 CEST 2006


abcd wrote:
> Daniel Evers wrote:
> 
>>Right. You can check this e.g. with
>>
>>        hasattr(x, "__getitem__")
>>
>>because the __getitem__ method is used for indexing.
> 
> Thanks...that is what I was looking for!

Maybe, but it's not a particularly Pythonic way of doing it.  Better is 
probably something like this:

try:
     x[0]
     x_is_subscriptable = True
except TypeError:
     x_is_subscriptable = False

-Peter




More information about the Python-list mailing list