3

I've seen multiple instances of this question like this one, but it fails to identify what exactly I am doing wrong since I don't have default arguments.

What am I doing wrong? Python object instantiation keeping data from previous instantiation?

#Table.py
class Table:

def __init__(self, players):
    self.deck = Deck()

And this is Main

t = Table(2)
print len(t.deck.cards)

t = Table(2)
print len(t.deck.cards)

I would expect this to print 48 each time, but instead it prints

48 and then 96

Why is this? Shouldn't this member variable be overridden every time?

#Deck.py
from Card import *
import random

class Deck:

suits = ['H','C','D','S']
numbers = [2,3,4,5,6,7,8,9,10,11,12,13,14]
cards = []

def __init__(self):
    for num in self.numbers:
        for suit in self.suits:
            c = Card(num,suit)
            self.cards.append(c);
    random.shuffle(self.cards)

Card.py

class Card:

def __init__(self, num, suit):
    self.num = num
    self.suit = suit

def __repr__(self):
    return str(self.num) + str(self.suit)

def __str__(self):
    return str(self.num) + str(self.suit)
gsamaras
  • 71,951
  • 46
  • 188
  • 305
k9b
  • 1,457
  • 4
  • 24
  • 54

3 Answers3

6

Initialize cards in the constructor, like this:

def __init__(self):
    self.cards = []
    for num in self.numbers:
        for suit in self.suits:
            c = Card(num,suit)
            self.cards.append(c);
    random.shuffle(self.cards)

That way, every time a new instance of the class is created, cards will be freshly initialized.

Your approach didn't work as you wished, since cards is a class data member, shared among all instances of class Deck.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Well @Penn There seems to be another problem now where the cards are being shuffled in the same order every time :) Working on that now https://stackoverflow.com/questions/4794663/python-list-does-not-shuffle-in-a-loop – k9b Sep 03 '17 at 18:59
2

suits, numbers and cards are class variables. So when doing self.cards.append(c) you add to a class variable, which is shared by all instances of all Deck instances.

Put them into __init__ instead:

def __init__(self):

    self.cards = []
    for num in self.numbers:
        for suit in self.suits:
            c = Card(num,suit)
            self.cards.append(c);
    random.shuffle(self.cards)
syntonym
  • 7,134
  • 2
  • 32
  • 45
-1

You are using class variables instead of instance variables. See, for example, python class variables

So even though you instantiate a new instance, you don't get a new instance of the static class variables.

Suits, numbers, cards. If you want instance variables, use "self.", and do it in the init function.

You are appending cards each time you instantiate, but you are appending them to the class variable. Thus you end up with twice as many.

Basya
  • 1,477
  • 1
  • 12
  • 22
  • Hey, I see that other people just after me might have worded things better or given code samples, I get it. But whoever downvoted me -- maybe you could tell me what's wrong so I'll do better next time? I think my answer is correct -- what am I missing? Thanks. – Basya Sep 03 '17 at 18:43
  • Yes, I guess it would be. But someone beat me to it so I don't think I'll repeat it -- it would be more or less the same. Still curious about the downvote....either I'll get an answer and learn from it or I'll be left wondering :-) It could be because I hit "post" too soon and immediately edited it, but I still think my initial fragment was not incorrect, just not descriptive enough. – Basya Sep 03 '17 at 19:10