Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created June 22, 2023 17:26
Show Gist options
  • Save stephengruppetta/9bad94544661386015f7b0b71695529f to your computer and use it in GitHub Desktop.
Save stephengruppetta/9bad94544661386015f7b0b71695529f to your computer and use it in GitHub Desktop.
class VendingMachine:
def __init__(self):
self.items_on_sale = []
def __getitem__(self, idx):
if isinstance(idx, int):
return self.items_on_sale[idx]
raise TypeError("Index should be an integer")
def add_items(self, items):
self.items_on_sale.extend(items)
hideous_machine = VendingMachine()
hideous_machine.add_items(
[
"Hazelnut Chocolate",
"Dark Chocolate",
"Still Water",
"Sparkling Water",
]
)
print(hideous_machine[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment