A note on heapq module

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Jan 26 14:06:49 EST 2007


bearophile:
> I don't like your solution, this class was already slow enough. Don't
> use unbound methods with this class :-)

Sorry for raising this discussion after so much time. Another possibile
solution is to use the normal methods for the normal case, and replace
them only if key is present (this reduces problems with unbound methods
but you may use the wrong method).

Example:

def __init__(self, sequence=None, key=None, inplace=False):
    ...
    if key is None:
        ...
    else:
        ...
        self.pop = self.__pop_key

    ...
def pop(self):
    return heappop(self._heap)
def __pop_key(self):
    return heappop(self._heap)[2]


Someone else has done something similar:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/e61ba58c9722bf51/

http://sourceforge.net/tracker/index.php?func=detail&aid=1162363&group_id=5470&atid=305470

Bye,
bearophile




More information about the Python-list mailing list