Created
January 5, 2021 19:55
-
-
Save vitalizzare/9a7adc04462b519fde09ee3e0507df3c to your computer and use it in GitHub Desktop.
Stack implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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