/mypy-guide-classes-1.py Secret
Created
May 5, 2021 16:36
This file contains 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
from typing import List | |
class Stack: | |
def __init__(self) -> None: | |
self._values: List[int] = [] | |
def __repr__(self) -> str: | |
return f'Stack{self._values!r}' | |
def push(self, value: int) -> None: | |
self._values.append(value) | |
def pop(self) -> int: | |
if len(self._values) == 0: | |
raise RuntimeError('Underflow!') | |
return self._values.pop() | |
stack = Stack() | |
print(stack) # Stack[] | |
stack.push(2) | |
stack.push(10) | |
print(stack) # Stack[2, 10] | |
print(stack.pop()) # 10 | |
print(stack) # Stack[2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment