this is my code:
class Plant:
def __init__(self,name):
self.name = name
class Garden:
def __init__(self,name):
self.name = name
list_of_plants = []
list_of_gardens = []
list_of_gardens.append(Garden("garden1"))
list_of_gardens.append(Garden("garden2"))
list_of_gardens[0].list_of_plants.append(Plant("Basil"))
print("Plant1: ", list_of_gardens[0].list_of_plants[0].name)
print("Plant2: ", list_of_gardens[1].list_of_plants[0].name)
Output:
Plant1: Basil
Plant2: Basil
Why does Basil appears in the two nested list? I didn't even affected a value in the second list! Even when I look at the pointers, everything looks good, but append keeps adding values in my other nested lists.