Skip to main content

Considering this, I would say it's just plain unpythonic to implement a stack structure; instead, just use a list, that really is a dequeue and thus can be used as both a stack and a queue.

On the other hand, the inheritance I suggested happens to violate Liskov's subsitutionsubstitution principle. Although there's no problem with adding push and peek methods with respect to this principle, my implementation changes the signature of the pop method, which is not allowed by this principle.

On the Liskov substitution principle:

Considering this, I would say it's just plain unpythonic to implement a stack structure; instead just use a list, that really is a dequeue and thus can be used as both a stack and a queue.

On the other hand, the inheritance I suggested happens to violate Liskov's subsitution principle. Although there's no problem with adding push and peek methods with respect to this principle, my implementation changes the signature of the pop method, which is not allowed by this principle.

On Liskov substitution principle:

Considering this, I would say it's just plain unpythonic to implement a stack structure; instead, just use a list, that really is a dequeue and thus can be used as both a stack and a queue.

On the other hand, the inheritance I suggested happens to violate Liskov's substitution principle. Although there's no problem with adding push and peek methods with respect to this principle, my implementation changes the signature of the pop method, which is not allowed by this principle.

On the Liskov substitution principle:

added 6 characters in body
Source Link
Right leg
  • 441
  • 4
  • 8
class Stack:
    def __init__(self):
        self._data = []

    def push(self, x):
        self._data.append(x)

    def pop(self):
        return self._data.pop()

    def peek(self)
        return self._data[-1]

    def __len__(self):
        return len(self._data)
class Stack:
    def __init__(self):
        self._data = []

    def push(self, x):
        self._data.append(x)

    def pop(self):
        return self._data.pop()

    def peek(self)
        return self._data[-1]

    def __len__(self):
        return len(self)
class Stack:
    def __init__(self):
        self._data = []

    def push(self, x):
        self._data.append(x)

    def pop(self):
        return self._data.pop()

    def peek(self)
        return self._data[-1]

    def __len__(self):
        return len(self._data)
added 428 characters in body
Source Link
Right leg
  • 441
  • 4
  • 8
added 1603 characters in body
Source Link
Right leg
  • 441
  • 4
  • 8
Loading
added 253 characters in body; added 42 characters in body; added 5 characters in body
Source Link
Right leg
  • 441
  • 4
  • 8
Loading
Source Link
Right leg
  • 441
  • 4
  • 8
Loading