Previous code is available at Python3 Stack implementation with List built-in, this is a second-round review after I made alterations.
Once again, let's review my Stack implementation, with built-in lists, and with review notes from the prior review implemented.
Any changes and suggestions are welcome!
class Stack:
def __init__(self):
self._stack = []
def __len__(self):
return len(self._stack)
def pop(self):
if len(self) == 0:
raise IndexError("You cannot pop from an empty stack.")
return self._stack.pop()
def peek(self):
if len(self) == 0:
raise IndexError("You cannot peek into an empty stack.")
return self._stack[len(self) - 1]
def push(self, item):
self._stack.append(item)
def __iter__(self):
return iter(self._stack)
__iter__
iterate the list in reverse order? \$\endgroup\$