Tuple question

Mel Wilson mwilson at the-wire.com
Tue Sep 7 00:46:13 EDT 2004


In article <roy-D04C66.10354305092004 at reader1.panix.com>,
Roy Smith <roy at panix.com> wrote:
>So, to restate my original question, why should my mutable,
>content-based-eqality class instance be a valid dictionary key, when a
>list is not?  Which part of a list's behavior makes it inherently
>unusable as a key?  I'm not asking about design philosophy, I'm asking
>about observable behavior.

      a = [1,2,3,4,5]
      D = {a:'First five natural numbers'}

The hash of the list should be based on value, so that after

      b = [1,2,3,4,5]
      c = D[b]

c would be 'First five natural numbers', even though b is
not the same object as a.

Now suppose

      a.append(6)

What then happens on

      e = D[b]

b no longer equals a, so the existing dictionary item can't
match b as a dictionary key.  Moreover, the relevant
dictionary item is filed under hash([1,2,3,4,5]) and not
under hash([1,2,3,4,5,6]), so trying to access D with a key
equal to the appended-to value of a won't hash to the right
place to find a's item.  Trouble.

If you roll your own hashable class, it's assumed you have
thought about these issues.

        Regards.        Mel.



More information about the Python-list mailing list