Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python raise TypeError rather than SyntaxError?

Tags:

python

A question purely for curiosity's sake. This is obviously invalid syntax:

foo = {} foo['bar': 'baz'] 

It's obvious what happened, the developer moved a line out of the dictionary definition but didn't change it from the literal dictionary declaration to the assignment syntax (and has been suitably mocked as a result).

But my question is, why does Python raise TypeError: unhashable type here rather than SyntaxError? What type is it attempting to hash? Just doing this:

'bar': 'baz' 

is a SyntaxError, as is this:

['bar': 'baz'] 

so I can't see what type is being created that is unhashable.

like image 529
Daniel Roseman Avatar asked Nov 11 '10 17:11

Daniel Roseman


1 Answers

Using the colon in an indexing operation generates a slice object, which is not hashable.

like image 192
Ignacio Vazquez-Abrams Avatar answered Sep 27 '22 20:09

Ignacio Vazquez-Abrams