I have 2 issues with the code below:
Should I throw an exception if pop()
You are right to use composition instead of inheritance, because inheritance brings methods in that you don't want to expose.
class Stack:
def __init__(self):
self.__storage = []
def isEmpty(self):
return len(self.__storage) == 0
def push(self,p):
self.__storage.append(p)
def pop(self):
return self.__storage.pop()
This way your interface works pretty much like list
(same behavior on pop
for example), except that you've locked it to ensure nobody messes with the internals.