<div dir="ltr">I agree with Paul -- I'm not convinced that this is common enough or that the benefits are big enough to warrant something builtin. However, I did decide to dust off some of my old skills and I threw together a simple gist to see how hard it would be to create something using Cython based on the CPython tuple hash algorithm. I don't know how well it works for arbitrary iterables without a `__length_hint__`, but seems to work as intended for iterables that have the length hint.<div><a href="goog_827102756"><br></a></div><div><a href="https://gist.github.com/mgilson/129859a79487a483163980db25b709bf">https://gist.github.com/mgilson/129859a79487a483163980db25b709bf</a><br></div><div><br></div><div>If you're interested, or want to pick this up and actually do something with it, feel free... Also, I haven't written anything using Cython for ages, so if this could be further optimized, feel free to let me know.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 5, 2017 at 7:58 AM, Paul Moore <span dir="ltr"><<a href="mailto:p.f.moore@gmail.com" target="_blank">p.f.moore@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 5 January 2017 at 13:28, Neil Girdhar <<a href="mailto:mistersheik@gmail.com">mistersheik@gmail.com</a>> wrote:<br>
> The point is that the OP doesn't want to write his own hash function, but<br>
> wants Python to provide a standard way of hashing an iterable. Today, the<br>
> standard way is to convert to tuple and call hash on that. That may not be<br>
> efficient. FWIW from a style perspective, I agree with OP.<br>
<br>
</span>The debate here regarding tuple/frozenset indicates that there may not<br>
be a "standard way" of hashing an iterable (should order matter?).<br>
Although I agree that assuming order matters is a reasonable<br>
assumption to make in the absence of any better information.<br>
<br>
Hashing is low enough level that providing helpers in the stdlib is<br>
not unreasonable. It's not obvious (to me, at least) that it's a<br>
common enough need to warrant it, though. Do we have any information<br>
on how often people implement their own __hash__, or how often<br>
hash(tuple(my_iterable)) would be an acceptable hash, except for the<br>
cost of creating the tuple? The OP's request is the only time this has<br>
come up as a requirement, to my knowledge. Hence my suggestion to copy<br>
the tuple implementation, modify it to work with general iterables,<br>
and publish it as a 3rd party module - its usage might give us an idea<br>
of how often this need arises. (The other option would be for someone<br>
to do some analysis of published code).<br>
<br>
Assuming it is a sufficiently useful primitive to add, then we can<br>
debate naming. But I'd prefer it to be named in such a way that it<br>
makes it clear that it's a low-level helper for people writing their<br>
own __hash__ function, and not some sort of variant of hashing (which<br>
hash.from_iterable implies to me).<br>
<span class="HOEnZb"><font color="#888888"><br>
Paul<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><span><br><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:14.6667px;font-family:Arial;color:rgb(0,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><img src="https://lh6.googleusercontent.com/KjKhxPuGD6aSETVh9dazBSiS5FGrt8b37DYQ6QZAxpJIomTEygFF6ygbgOUbR0MBzycO8-LF-FspB3wTJb8LXEfIbJ6lN4H2J04EYzOZg7uaG7YcGkWesdldHuXzhLwS-etJBGO6" width="95" height="26" style="border:none" alt="pattern-sig.png"></span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:10.6667px;font-family:Arial;color:rgb(102,102,102);font-weight:700;vertical-align:baseline;white-space:pre-wrap">Matt Gilson</span><span style="font-size:10.6667px;font-family:Arial;color:rgb(102,102,102);vertical-align:baseline;white-space:pre-wrap"> // SOFTWARE ENGINEER</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt"><span style="font-size:10.6667px;font-family:Arial;color:rgb(153,153,153);vertical-align:baseline;white-space:pre-wrap">E: </span><span style="font-size:10.6667px;font-family:Arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap"><a href="mailto:matt@getpattern.com" target="_blank">matt@getpattern.com</a></span><span style="font-size:10.6667px;font-family:Arial;color:rgb(153,153,153);vertical-align:baseline;white-space:pre-wrap"> // P: 603.892.7736</span></p><br><span style="font-size:12px;font-family:Arial;color:rgb(255,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent">We’re looking for beta testers. Go </span><a href="https://www.getpattern.com/meetpattern" style="text-decoration:none" target="_blank"><span style="font-size:12px;font-family:Arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent">here</span></a><span style="font-size:12px;font-family:Arial;color:rgb(255,0,0);vertical-align:baseline;white-space:pre-wrap;background-color:transparent"> to sign up!</span></span><br></div></div>
</div>