0

So ive got a class that looks like this:

class Item:
    name: str = None  # The name of the item
    [...]
    requiredIngredients: list = []  # A list of ingredients required to make/use this item

    def __init__(self, name):
        self.name = name

I then create an object of that class like this

stone = Item("Stone")

When i check stone.requiredIngredients its empty, as expected here. But then I add another object

pillar = Item("Pillar")
pillar.requiredItems.append(stone)

Now when i check stones required ingredients, id expect it to still be empty, as ive modified Pillar, not stone, but instead i find stone.requiredIngredients and pillar.requiredIngredients are identical

Am i doing something wrong here? How can i append an objects list without updating other objects?

  • 1
    ``requiredIngredients`` is a *class* attribute. There is only one for the class, not a separate list per instance. – MisterMiyagi Apr 01 '20 at 11:38
  • Perfectly, thank you. Can you make that an answer so i can mark the answer? –  Apr 01 '20 at 11:41
  • 1
    No worries, just wait a bit until the question has been [marked as duplicate](https://stackoverflow.com/help/duplicates). This helps keep answers consistent. – MisterMiyagi Apr 01 '20 at 11:46

0 Answers0