- Mar 22, 2022
- 14
- 114
Hello. I'm at my wit's end, so I'd appreciate it if you could enlighten me as to why this isn't working. For a bit of context - I have a list of object Card called drawnCards. The Card object has two fields name and is_selected. This "is_selected" field, depending on it's state, will show different visuals. So I'm trying to copy over cards from drawnCards to another list called usedCards. I'm trying to copy over the objects by using copy/deepcopy or just creating a new instance of Card by using a copy builder method, but it's just not working.
to use deepcopy I've used "from copy import deepcopy" at the begining of the python code init
The behavior is still the same no matter which of the two methods I use. When I change the name/is_selected value it reflects on the objects in both lists,even though supposedely i'm not copying them by reference. I'd appreciate any help/feedback or ideas. Thanks!
Python:
def cloneCard(self):
return Card(self.name, self.selected_card)
def copyCard(self):
newCard = deepcopy(self)
return newCard
def addCardToHistory(card):
if len(usedCards) < 6:
usedCards.append(card.cloneCard())
#usedCards.append(card.clone())
else:
usedCards.pop(0)
usedCards.append(card.cloneCard())
#usedCards.append(card.clone())
The behavior is still the same no matter which of the two methods I use. When I change the name/is_selected value it reflects on the objects in both lists,even though supposedely i'm not copying them by reference. I'd appreciate any help/feedback or ideas. Thanks!