<div dir="ltr">This has been brought up multiple times. Last time was on this thread <a href="https://mail.python.org/pipermail/python-ideas/2016-October/043024.html">https://mail.python.org/pipermail/python-ideas/2016-October/043024.html</a>.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 21, 2017 at 3:13 AM, bunslow <span dir="ltr"><<a href="mailto:bunslow@gmail.com" target="_blank">bunslow@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Nothing so bombastic this time. The heapq functions are basically all named "heapsomething", and basically all take a "heap" for their first argument, with supplementary args coming after. It's a textbook example of the (hypothetical) Object Oriented Manifesto™ where defining a class increases type safety and programmers' conceptual clarity. There're practically no drawbacks, and the code to be added would be very simple. Updating the tests and docs would probably be harder.<div><br></div><div>In pure Python, such a class would look like this:</div><div><br></div><div><div>class Heap(list):</div><div><br></div><div> def __init__(self, iterable=None):</div><div> if iterable:</div><div> super().__init__(iterable)</div><div> else:</div><div> super().__init__()<br><br></div><div> self.heapify()</div><div><br></div><div> push = heapq.heappush</div><div> pop = heapq.heappop</div><div> pushpop = heapq.heappushpop</div><div> replace = heapq.heapreplace</div><div> heapify = heapq.heapify</div><div><br></div><div> # This could be a simple wrapper as well, but I had the following thoughts anyways, so here they are</div><div> def nsmallest(self, n, key=None):</div><div> # heapq.nsmallest makes a *max* heap of the first n elements,</div><div> # while we know that self is already a min heap, so we can</div><div> # make the max heap construction faster</div><div> self[:n] = reversed(self[:n])</div><div> return heapq.nsmallest(n, self, key)</div><div><br></div><div> # do we define nlargest on a minheap??<br><br><br>Wrapping around the C builtin functions (which aren't descriptors) would be a bit harder, but not much so:</div></div><div><br></div><div>from functools import partial</div><div><br></div><div>class Heap(list):</div><div> def __init__(self, iterable=None):</div><div> if iterable:</div><div> super().__init__(iterable)</div><div> else:</div><div> super().__init__()</div><div><br></div><div> self.heapify = partial(heapq.heapify, self)</div><div> self.push = partial(heapq.heappush, self)</div><div> ...</div><div><br></div><div> self.heapify()</div><div><br></div><div><br></div><div>Thoughts?</div></div>
<br>______________________________<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>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Sebastian Kreft</div>
</div>