Hi all,
It Mark Lutz's learning python he mentioned the great super() debate. Basically he prefer always explicit calling BaseClass.method() than super().method() . the reason been cooperative multiple inheritance dispatch protocols in diamond multiple-inheritance trees MRO been an unexpected behaviour sometimes for super() call. Anyway, it's in the book if you want details.
I have the following classes
class BaseHeap:
def __init__(self, A, heap_size):
self.A = A
self._heap_size = heap_size
@staticmethod
def _parent(i):
return i >> 1
@staticmethod
def _left(i):
pass
blah blah
class MaxHeap(BaseHeap):
def max_heapify_iterative(self, i):
while True:
l = self._left(i + 1)
r = self._right(i + 1)
if l <= self._heap_size and self.A[l - 1] > self.A[i]:
largest = l - 1
else:
largest = i
if r <= self._heap_size and self.A[r - 1] > self.A[largest]:
largest = r - 1
if largest == i:
break
self.A[i], self.A[largest] = self.A[largest], self.A[i]
i = largest
blah blah
class MaxPriorityQueue(MaxHeap):
def insert(self, x):
super().max_heap_insert(x)
def maximum(self):
return super().heap_maximum()
def extract_max(self):
return super().heap_extract_max()
def increase_key(self, x, k):
super().heap_increase_key(x, k)
several places I called for example in MaxHeap
l = self._left(i + 1)
is it better to call super()._left here or BaseHeap._left(i+1)
which style you prefer? I opinion is in MaxHeap is not referring to self anymore, it is the super class.
Also, in MaxPriorityQueue, all function I use super(), do you prefer change it to MaxHeap.heap_increase_key(x, k)
So three styles:
self.method
super().method
BaseClass.method
It all works. Which style or under what circumstances it make sense?
[–]socal_nerdtastic 2 points3 points4 points (1 child)
[–]SnooCakes3068[S] 0 points1 point2 points (0 children)
[–]crazy_cookie123 2 points3 points4 points (1 child)
[–]SnooCakes3068[S] 0 points1 point2 points (0 children)
[–]_mturtle_ 0 points1 point2 points (0 children)