[Python-Dev] Re: heapq method names

Tim Peters tim.one@comcast.net
Sun, 25 Aug 2002 12:53:24 -0400


Note that it's common to use the bisect module in the

    from bisect import bisect_right, bisect, insort

way too, rather than spell out bisect.bisect (etc) each time.  That's "the
other" module that (conceptually) adds new methods to lists.

If you want simpler names, I'm finding this little module quite pleasant to
use:

"""
import heapq

class Heap(list):
    def __init__(self, iterable=[]):
        self.extend(iterable)
    push    = heapq.heappush
    popmin  = heapq.heappop
    replace = heapq.heapreplace
    heapify = heapq.heapify
"""

That is, it creates a Heap type that's just a list with some extra methods.
Note that the "pop" method can't be named "pop"!  If you try, you'll soon
get unbounded recursion because the heapq functions need list.pop to access
the list meaning of "pop".

Guido suggested a long time ago that such a class could be added to heapq,
and I like it a lot in real life.