Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
Created January 5, 2021 19:55
Show Gist options
  • Select an option

  • Save vitalizzare/9a7adc04462b519fde09ee3e0507df3c to your computer and use it in GitHub Desktop.

Select an option

Save vitalizzare/9a7adc04462b519fde09ee3e0507df3c to your computer and use it in GitHub Desktop.
Stack implementation
# New Stack Implementation
import logging
class Stack:
def __init__(self):
self._items = []
def push(self, item):
self._items.append(item)
def pop(self):
try:
return self._items.pop()
except IndexError:
msg = "Trying to pop data from an empty stack."
logging.warning(msg)
raise IndexError(msg)
def __len__(self):
return len(self._items)
def __repr__(self):
return f"{self.__class__.__name__}()"
def __str__(self):
return f"{self.__class__.__name__}({self._items})"
# https://realpython.com/python-append/#implementing-a-stack
class Stack:
def __init__(self):
self._items = []
def push(self, item):
self._items.append(item)
def pop(self):
try:
return self._items.pop()
except IndexError:
print("Empty stack")
def __len__(self):
return len(self._items)
def __repr__(self):
return f"Stack({self._items})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment