[issue32142] heapq.heappop - documentation misleading or doesn't work
New submission from Scott Queen : The documentation for heapq.heappop(heap) says: "Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty, IndexError is raised. To access the smallest item without popping it, use heap[0]." yet, in the following code, the resultant heap doesn't reflect the heap invariant: import heapq li = [5, 7, 9, 1, 4, 3] heapq.heapify(li) #change a couple values in the heap li[3] = 16 li[4] = 2 print (heapq.heappop(li)) print ("The heap after pop is : ",end="") print (list(li)) This prints: The heap after pop is : [3, 4, 9, 16, 2] The documentation implies to me that heapify would be called internally after heappop, but I may be misreading. Perhaps heappop could say that the heap invariant is maintained if the heap is properly sorted before the heappop invocation. -- assignee: docs@python components: Documentation messages: 307006 nosy: docs@python, scooter4j priority: normal severity: normal status: open title: heapq.heappop - documentation misleading or doesn't work type: behavior versions: Python 3.6 ___ Python tracker <https://bugs.python.org/issue32142> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32142] heapq.heappop - documentation misleading or doesn't work
Scott Queen added the comment: Fair statement. "Properly sorted" was a poor choice. Seems that "if the invariant is true as a precondition, it will be true as a postcondition" is accurate and descriptive - maybe with a caution that modifying the list (heap) values by means other than the heapq functions are likely to render the invariant false (the latter phrase being for somewhat newbies like me who wouldn't realize this truth without having to learn it in the debugger). On Sun, Nov 26, 2017 at 10:51 AM, abcdef wrote: > > abcdef added the comment: > > > Perhaps heappop could say that the heap invariant is maintained if the > heap is properly sorted before the heappop invocation. > > Honestly I like this wording less. "Properly sorted" would suggest sorted > in the sense of heap.sort() [as the docs refer to this earlier], but the > array doesn't have to be sorted; it's enough if it's a heap. The function > always maintains the invariant - this means that if the invariant is true > as a precondition, it will be true as a postcondition. Maybe you have a > different idea? > > -- > nosy: +abcdef > > ___ > Python tracker > <https://bugs.python.org/issue32142> > ___ > -- ___ Python tracker <https://bugs.python.org/issue32142> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com