I have a question regarding about inheritance between the child class and the parent class. Currently, my current __Stack__push()
function in my child class Stack3
is unable to overwrite the parent push function. As far as I know from my understanding, the child class is supposed to overwrite what has been defined in the parent class. However, in this case it is not happening.
class Stack:
def __init__(self):
self.__list= []
def isEmpty(self):
return self.__list == []
def size(self):
return len(self.__list)
def clear(self):
self.__list.clear()
def push(self, item):
self.__list.append(item)
def pop(self): # popTail
if self.isEmpty():
return None
else:
return self.__list.pop()
def get(self):
if self.isEmpty():
return None
else:
return self.__list[-1]
def __str__(self):
output = '<'
for i in range( len(self.__list) ):
item = self.__list[i]
if i < len(self.__list)-1 :
output += f'{str(item)}, '
else:
output += f'{str(item)}'
output += '>'
return output
class Stack3(Stack):
def __init__(self,list):
super().__init__()
def push(self, item):
super().push(item)
self.__list = []
self.__list.insert(0,item)
def pop(self):
super().pop()
if self.isEmpty():
return None
else:
return self.__list.pop(0)
# main programme
s= Stack3(Stack())
print(s.pop())
for i in range(1,6):
s.push(i)
print('Content of stack =',s)
print('Item at top=',s.get())
print('Size=', s.size())
while not s.isEmpty():
print(s.pop())
print(s)
The output was supposed to look something like this:
None
Content of stack = <5, 4, 3, 2, 1>
Item at top= 5
Size= 5
5
<4, 3, 2, 1>
4
<3, 2, 1>
3
<2, 1>
2
<1>
1
<>
But my current one looks like this
None
Content of stack = <1, 2, 3, 4, 5>
Item at top= 5
Size= 5
5
<1, 2, 3, 4>
And thereafter it gives me this error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-1-1eecf96bf9ca> in <module>
66 print('Size=', s.size())
67 while not s.isEmpty():
---> 68 print(s.pop())
69 print(s)
<ipython-input-1-1eecf96bf9ca> in pop(self)
53 return None
54 else:
---> 55 return self.__list.pop(0)
56
57 # main programme
IndexError: pop from empty list
So, after trials and errors and googling, here's my answer. Please feel free to comment what can be modified!
class Stack:
def __init__(self):
self.__list= []
def isEmpty(self):
return self.__list == []
def size(self):
return len(self.__list)
def clear(self):
self.__list.clear()
def push(self, item):
self.__list.append(item)
def pop(self): # popTail
if self.isEmpty():
return None
else:
return self.__list.pop()
def get(self):
if self.isEmpty():
return None
else:
return self.__list[-1]
def __str__(self):
output = '<'
for i in range( len(self.__list) ):
item = self.__list[i]
if i < len(self.__list)-1 :
output += f'{str(item)}, '
else:
output += f'{str(item)}'
output += '>'
return output
class Stack3(Stack):
def __init__(self,list):
super().__init__()
self.__list = []
def push(self, item):
super().push(item)
self.__list.insert(0,item)
def isEmpty(self):
super().isEmpty()
return self.__list == []
def pop(self):
super().pop()
if self.isEmpty():
return None
else:
return self.__list.pop(0)
def __str__(self):
output = '<'
for i in range( len(self.__list) ):
item = self.__list[i]
if i < len(self.__list)-1 :
output += f'{str(item)}, '
else:
output += f'{str(item)}'
output += '>'
return output
# main programme
s= Stack3(Stack())
print(s.pop())
for i in range(1,6):
s.push(i)
print('Content of stack =',s)
print('Item at top=',s.get())
print('Size=', s.size())
while not s.isEmpty():
print(s.pop())
print(s)