[Python-ideas] incremental hashing in __hash__
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Wed Jan 4 18:39:52 EST 2017
jab at math.brown.edu writes:
> Instead of the proposals like "hash.from_iterable()", would it make sense
> to allow tuple.__hash__() to accept any iterable, when called as a
> classmethod?
[...]
> Would this API more cleanly communicate the algorithm being used and the
> implementation, while making a smaller increase in API surface area
> compared to introducing a new function?
I don't understand what you're proposing. There are three problems
with the API. First, the "obvious" meaning of
"tuple.__hash__(iterable)" is "hash(tuple(iterable))", and that's the
obvious spelling. Second, there's no reason for a dunder method to be
polymorphic; this is hardly discoverable. Third, dunders are normally
not called from user code (including class implementations, although
they're less ugly there), suggesting that there should be a helper for
this.
I'm unclear on how the function is supposed to be implemented, since
presumably tuple.__hash__ knows about the tuple data structure's
implementation (memory layout), but it can't know about an arbitrary
iterable.
Steve
More information about the Python-ideas
mailing list