During experimenting, I had encountered a really bizarre problem which made me very confused. After creating several class objects by iterating through a range, I called a function from another class that would append 1 string to the primary class objects. After looking at all of the objects' lists, I saw that they were all the same, even though they should've been different, and there wasn't just one string for each class, but the number of strings matched the number of the class objects themselves.
class Stack():
cards = []
setUp = False
def set_up(self):
cardTypes = ["A", "B", "C", "D"]
for cardType in cardTypes:
for x in range(3):
self.cards.append(cardType)
self.setUp = True
def deal(self, player, amount):
if self.setUp:
for x in range(amount):
card = self.cards[0]
self.cards.remove(card)
player.cards.append(card)
else:
self.set_up()
self.deal(player, amount)
class Player():
cards = []
class Game():
def start(self):
stack = Stack()
players = [Player() for player in range(int(input("How many players?")))]
for player in players:
stack.deal(player, 1)
for player in players:
print(player.cards)
#all players have the same numbers of cards (the amount of players = number of cards)
#when all players should only have 1 card each since I put 1 in stack.deal
#it's also as if all players become one when I call that function.
I ran the code using Game().start()
.