For the code below, the inventory in add_item gets bound to the function like detailed here:
"Least Astonishment" and the Mutable Default Argument
class Person:
def __init__(self, name: str, inventory: List[Item] = []):
self.name = name
self.inventory = inventory
def add_item(self, item: Item):
self.inventory.append(item)
So this is what happens:
Dave.add_item(Book) # has Book
Mary.add_item(Apple) # has Book and Apple
So I understand what's going on now but I want Mary to only have the Apple and I don't know how to fix it so that works.
Reading further down that page, I found that doing self.inventory = []
works but that would prevent me from initializing items to people in their constructors which I would like to keep since my add_item method only adds one thing at a time.