Bug in Python?

Sven R. Kunze srkunze at mail.de
Fri Feb 26 17:08:41 EST 2016


Hi everybody,

I recognized the following oddity (background story: 
http://srkunze.blogspot.com/2016/02/lets-go-down-rabbit-hole.html).

Python sometimes seems not to hop back and forth between C and Python code.

Can somebody explain this?


class MyList(list):
     count = 0
     def __setitem__(self, key, value):
         self.count += 1
         super(MyList, self).__setitem__(key, value)

# using heapq directly
from heapq import heappop
ml = MyList(range(10))
heappop(ml)                 # that's C
print(ml.count) # print 0


# using exact copy from heapq
from heapq import _siftup
def my_heappop(heap):
     lastelt = heap.pop()
     if heap:
         returnitem = heap[0]
         heap[0] = lastelt
         _siftup(heap, 0)    # that's C
         return returnitem
     return lastelt

ml = MyList(range(10))
my_heappop(ml)
print(ml.count) # print 6


Best,
Sven




More information about the Python-list mailing list