hashkey/digest for a complex object

Terry Reedy tjreedy at udel.edu
Wed Oct 6 17:43:00 EDT 2010


On 10/6/2010 2:58 PM, kj wrote:

> These objects are non-mutable once they are created,

See below.

> like to use a two-step comparison for equality, based on the
> assumption that I can compute (either at creation time, or as needed
> and memoized) a hashkey/digest for each object.  The test for
> equality of two of these objects would first compare their hashkeys.
> If they are different, the two objects are declared different; if
> they match, then a more stringent test for equality is performed.

I believe Python strings do this (cache the hash). Equality comparison 
can check length, hashes, and only then chars.

> So the problem is to come up with a reasonable hashkey for each of
> these objects.  The objects have two significant attributes, and
> two of these objects should be regarded as equal if these attributes
> are "the same" in both.  The first attribute is a simple dictionary
> whose keys are integers and values are strings.  The second attribute
> is more complicated.  It is a tree structure, represented as a
> dictionary of dictionaries of dictionaries... until we get to the
> leaf elements, which are frozensets of strings.  The keys at every
> level of this structure are strings.  E.g. a simple example of such
> an attribute would look like:
>
> {'A': {'a': set(['1', '2', '3']),
>         'b': set(['4', '5'])},
>   'B': set(['6', '7', '8'])}

If these two attributes, and hence the dicts, are public, then your 
instances are mutable.

-- 
Terry Jan Reedy




More information about the Python-list mailing list