0
import CardDeck
from CardDeck import player
from CardDeck import deck
from CardDeck import hand

game_deck=deck()

def create_new_deck():
    global game_deck

    game_deck=deck()

    game_deck.shuffle(100)
    print "Creating new Deck...."

I would like for the create_new_deck() function to create a completely new instance of a deck object and use the old game_deck reference to refer to this new object. However, what happens is that python just calls the __init__() function again on the old instance of the object reffered to by game_deck.

How do I create a new instance of a class using an old variable name in Python?

The deck class looks like this

class deck(object):

    cardList=[]

    #creates 52 card deck
    def __init__(self):

        print "*******creating new deck*******"
        for i in range(1,14):
            self.cardList.append(card("spade",i))
        print "*******creating spades*******"

        for i in range(1,14):
            self.cardList.append(card("heart",i))
        print "*******creating hearts*******"

        for i in range(1,14):
            self.cardList.append(card("diamond",i))
        print "*******creating diamonds*******"
        for i in range(1,14):
            self.cardList.append(card("club",i))
        print "*******creating clubs*******"
poke
  • 369,085
  • 72
  • 557
  • 602
Nicholas Booth
  • 49
  • 1
  • 10
  • 2
    *“python just calls the `__init__()` function again on the old instance of the object reffered to by game_deck”* – Where did you get that impression? There is no way your code does that. If `deck` is a type, then calling `deck()` will create a new object. – poke Feb 18 '15 at 07:34

1 Answers1

1

game_deck is a name. By doing game_deck = something, you are assigning something to the variable. If that something is different to what game_deck was referencing before, then you have changed the variable.

Since you use global, you do access the global game_deck variable, so as soon as you do game_deck = deck(), you are assigning the return value of deck() to the variable. And unlike deck() returns the original game_deck value, this changes the variable.

If deck is a type, then deck() will create a new object of that type and call the initializator on the new object. There is no way that this would call __init__ again on the old object.

poke
  • 369,085
  • 72
  • 557
  • 602
  • deck is a type, in the __init__() function of deck the program creates creates 52 cards and appends them in a list. Later when I use the create_new_deck() function above. A new list is not created, it just appends cards to the old list. Why should it do this when i created a whole new deck object using: game_deck=deck()? – Nicholas Booth Feb 18 '15 at 07:49
  • 1
    @NicholasBooth Then your type definition is wrong. Please show what you are doing there. But if I were to guess, then you probably don’t create the list as an instance member. You need to create the list `self.init = []` within the `__init__`. – poke Feb 18 '15 at 07:51
  • @NicholasBooth [See this question](http://stackoverflow.com/questions/1680528/how-do-i-avoid-having-python-class-data-shared-among-instances). – poke Feb 18 '15 at 07:53
  • Ah, okay that worked!, Im so used to java ! Thanks! – Nicholas Booth Feb 18 '15 at 07:56