As pointed out, you are confusing class attribute to instance attributes.
In you exemple, suitList and number are class attribute, they are shared among all the instance(c1, c2, c3)
When you change a class attribute as in c1.suitList[0] = "Heart"
, it will reflect in all class instances.
To fix this you have some options: I give you two.
1) Use only instance attributes:
class Card:
def __init__(self,number=0,suit="CLubs"):
self.number = number
self.suit = suit
def __str__(self):
return "%s %d"%(self.suit,self.number)
c1 = Card()
c2 = Card()
c1.suit = "Heart"
c1.number = 3
print c1
print c2
In this case, there's no class attribute, to change a card suite you assign it direcly using c1.suit.
2) Use a mix of class/attribute:
class Card:
suitList = ["CLubs", "Heart"]
def __init__(self,number=0,rank=0):
self.rank = rank
self.number = number
def __str__(self):
return (Card.suitList[self.rank] + " " + str(self.number))
c1 = Card()
c2 = Card()
c1.rank = 1
c1.number = 3
print c1
print c2
Rank in this case is a index that looks in the suitList. To change the Suit of card, you change it's rank, not the suitList.
Both exemple output:
Heart 3
CLubs 0