performance of tuple-less Python?

Eric Jacobs x at x.x
Mon Nov 29 17:35:36 EST 1999

Gordon McMillan wrote:
> I'm guessing that not being able to find something you stuck
> in the dictionary qualifies as a performance penalty ;-).
> To spell it out:
>  1) hash based on id(list_object):
>      d = {[1] = 'x'}
>      d[[1]] -> KeyError

Actually, this behavior can be useful at times. I was working on a graphviz
grapher to plot Python's internal ASTs. If I converted them to lists, Python
would complain becauses lists are not hashable. But when I used tuples, I saw
that Python was cleverly performing common subexpression analysis. So that in

  f(x + 1, x + 1, x + 1)

there would be only one subtree representing "x + 1", pointed to three times.
I had to explicitly state the id() operation to get it to the way (I thought)
it should look.

>  2) hash based on value:
>      lst = [1]
>      d[lst] = 'x'
>      lst.append(2)

The penalty would be that the dictionary would have to be reindexed at this
point, because the hash and compare behavior of lst would have changed.

>      d[lst] -> KeyError
> - Gordon

More information about the Python-list mailing list