[Python-ideas] incremental hashing in __hash__
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Thu Jan 5 21:10:31 EST 2017
Paul Moore writes:
> The debate here regarding tuple/frozenset indicates that there may not
> be a "standard way" of hashing an iterable (should order matter?).
If part of the data structure, yes, if an implementation accident, no.
> Although I agree that assuming order matters is a reasonable
> assumption to make in the absence of any better information.
I don't think so. Eg, with dicts now ordered by insertion, an
order-dependent default hash for collections means
a = {}
b = {}
a['1'] = 1
a['2'] = 2
b['2'] = 2
b['1'] = 1
hash(a) != hash(b) # modulo usual probability of collision
(and modulo normally not hashing mutables). For the same reason I
expect I'd disagree with Neil's proposal for an ImmutableWhatever
default __hash__ although the hash comparison is "cheap", it's still a
pessimization. Haven't thought that through, though.
BTW, it occurs to me that now that dictionaries are versioned, in some
cases it *may* make sense to hash dictionaries even though they are
mutable, although the "hash" would need to somehow account for the
version changing. Seems messy but maybe someone has an idea?
Steve
More information about the Python-ideas
mailing list