Bug in Python?

Ian Kelly ian.g.kelly at gmail.com
Fri Feb 26 17:37:15 EST 2016

On Fri, Feb 26, 2016 at 3:08 PM, Sven R. Kunze <srkunze at mail.de> wrote:
> Python sometimes seems not to hop back and forth between C and Python code.

C code as a rule tends to ignore dunder methods. Those are used to
implement Python operations, not C operations.

>         _siftup(heap, 0)    # that's C

Your comment here appears to be incorrect.

>>> from heapq import _siftup
>>> type(_siftup)
<class 'function'>
>>> import inspect
>>> print(inspect.getsource(_siftup))
def _siftup(heap, pos):
    endpos = len(heap)
    startpos = pos
    newitem = heap[pos]
    # Bubble up the smaller child until hitting a leaf.
    childpos = 2*pos + 1    # leftmost child position
    while childpos < endpos:
        # Set childpos to index of smaller child.
        rightpos = childpos + 1
        if rightpos < endpos and not heap[childpos] < heap[rightpos]:
            childpos = rightpos
        # Move the smaller child up.
        heap[pos] = heap[childpos]
        pos = childpos
        childpos = 2*pos + 1
    # The leaf at pos is empty now.  Put newitem there, and bubble it up
    # to its final resting place (by sifting its parents down).
    heap[pos] = newitem
    _siftdown(heap, startpos, pos)

So I would guess that the difference here is because one
implementation is entirely C, and the other implementation is entirely

More information about the Python-list mailing list