How to determine an object is "scriptable"

A

abcd

I recently came across a problem where I saw this error:
"TypeError: unsubscriptable object"

How can I determine if an object is "scriptable" or "unscriptable"?
 
R

Richard Brodie

I recently came across a problem where I saw this error:
"TypeError: unsubscriptable object"

How can I determine if an object is "scriptable" or "unscriptable"?

subscriptable: supports an indexing operator, like a list does.
 
A

abcd

Richard said:
subscriptable: supports an indexing operator, like a list does.

doesn't seem to be a builtin function or module...or is that just your
definition of subscriptable?
 
L

Larry Bates

abcd said:
I recently came across a problem where I saw this error:
"TypeError: unsubscriptable object"

How can I determine if an object is "scriptable" or "unscriptable"?
Simplest answer is to use isinstance to see if it is a string, list,
or tuple:

if isinstance(variable, (str, list, tuple)):
..
..
..

Now the problem is that this answer doesn't address the problem.
Your code is attempting to do something to the wrong type of
object/variable which is a logic problem.

-Larry Bates
 
D

Daniel Evers

Richard said:
subscriptable: supports an indexing operator, like a list does.

Right. You can check this e.g. with

hasattr(x, "__getitem__")

because the __getitem__ method is used for indexing.

Daniel
 
A

abcd

Daniel said:
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!
 
R

Richard Brodie

doesn't seem to be a builtin function or module...or is that just your
definition of subscriptable?

Yes, I figured you were just confused. You were using the wrong words,
after all.
 
B

bruno at modulix

Larry said:
Simplest answer is to use isinstance to see if it is a string, list,
or tuple:

if isinstance(variable, (str, list, tuple)):

Which will fail for a whole lot of objects that are scriptable...
Better use Daniel's solution or a try/except.
Now the problem is that this answer doesn't address the problem.
Your code is attempting to do something to the wrong type of
object/variable which is a logic problem.

Of course !-)
 
B

bruno at modulix

abcd said:
doesn't seem to be a builtin function or module...

It's not. Look no further.
or is that just your
definition of subscriptable?

It's a correct definition of 'subscriptable' in the context of the
Python programming language - as well as a good exemple of an implied
interface.
 
B

bruno at modulix

abcd said:
I recently came across a problem where I saw this error:
"TypeError: unsubscriptable object"

How can I determine if an object is "scriptable" or "unscriptable"?

By trying to apply the subscript operator ('[<index>]'). If it raises a
TypeError, then it's not subscriptable.

But, as Larry Bates already pointed out, your real problem is a logic
error: you get an unsubscriptable object where you assumed you had a
subscriptable one.
 
P

Peter Hansen

abcd said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top