Difference between unindexable and unsubscriptable

F

Fredrik Lundh

Jackson said:
What is the difference between "object is unindexable" and "object is
unsubscriptable"?

I would like to test if an object can accept: obj[0]
from sets import Set
Set([1,2])[0]
TypeError: unindexable object
TypeError: unsubscriptable object

It seems like each of these errors can be replaced with a single type error.

the message string is not part of the error type, and may change between
versions. just catch the TypeError and be done with it.

</F>
 
B

Ben Finney

Jackson said:
I would like to test if an object can accept: obj[0]

Then do so. Use the object in the way you want to use it, and catch
any exceptions that you want to handle.
from sets import Set
Set([1,2])[0]
TypeError: unindexable object
TypeError: unsubscriptable object

It seems like each of these errors can be replaced with a single
type error.

Well, they are both merely instances of TypeError, just with a
different message. Catching 'TypeError' will catch either of them.

I agree, though, that it would be preferable to have the same message
for these two cases that appear to be saying the same thing.
 
T

Theerasak Photha

Jackson said:
I would like to test if an object can accept: obj[0]

Then do so. Use the object in the way you want to use it, and catch
any exceptions that you want to handle.

I'm trying to learn about style conventions in Python. How would use
of getattr compare?

-- Theerasak
 
B

Ben Finney

Theerasak Photha said:
Jackson said:
I would like to test if an object can accept: obj[0]

Then do so. Use the object in the way you want to use it, and catch
any exceptions that you want to handle.

I'm trying to learn about style conventions in Python. How would use
of getattr compare?

I'm having trouble knowing what you need explained.

You have available to you an interactive Python interpreter, and the
documentation. Can you show us some sessions in the interpreter that
you still find confusing after you read the relevant documentation?
 
T

Theerasak Photha

I'm having trouble knowing what you need explained.

You have available to you an interactive Python interpreter, and the
documentation. Can you show us some sessions in the interpreter that
you still find confusing after you read the relevant documentation?

Don't worry, I got it now. (Ah Perl...how you warped my mind...)

-- Theerasak
 

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
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top