2

I am trying to understand object oriented programming. I am doing this by creating a small poker like program. I have come across a problem whose minimal working example is this:

For this code:

import random

class superthing(object):
    def __init__(self,name,listthing=[]):
        self.name = name
        self.listthing = listthing
    def randomlyadd(self):
        self.listthing.append(random.randint(1,50))
    def __str__(self):
        return '\nName: '+str(self.name)+'\nList: '+str(self.listthing)

Aboy = superthing('Aboy')
Aboy.randomlyadd()
print(Aboy)
Anotherboy = superthing('Anotherboy')
Anotherboy.randomlyadd()
print(Anotherboy)

I expect this output :

Name: Aboy
List: [44] 

(some number between 1 and 50)

Name: Anotherboy
List: [11] 

(again a random number between 1 and 50) But what I get is:

Name: Aboy
List: [44] 

(Meets my expectation)

Name: Anotherboy
List: [44,11] 

(it appends this number to the list in the previous instance) Why is this happening? The context is that two players are dealt a card from a deck. I am sorry if a similar question exists, if it does, I will read up on it if you can just point it out. New to stack overflow. Thanks in advance.

For the non minimal example, I am trying this:

import random

class Card(object):
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

    def getsuit(self):
        return self.suit

    def getval(self):
        return self.value

    def __str__(self):
        if(self.suit == 'Clubs'):
            suitstr = u'\u2663'
        elif(self.suit == 'Diamonds'):
            suitstr = u'\u2666'
        elif(self.suit == 'Hearts'):
            suitstr = u'\u2665'
        elif(self.suit == 'Spades'):
            suitstr = u'\u2660'
        if((self.value<11)&(self.value>1)):
            valuestr = str(self.value)
        elif(self.value == 11):
            valuestr = 'J'
        elif(self.value == 12):
            valuestr = 'Q'
        elif(self.value == 13):
            valuestr = 'K'
        elif((self.value == 1)|(self.value == 14)):
            valuestr = 'A'
        return(valuestr+suitstr)

class Deck(object):
    def __init__(self,DeckCards=[]):
        self.DeckCards = DeckCards

    def builddeck(self):
        suits = ['Hearts','Diamonds','Clubs','Spades']
        for suit in suits:
            for i in range(13):
                self.DeckCards.append(Card(suit,i+1))

    def shuffle(self):
        for i in range(len(self)):
            r = random.randint(0,len(self)-1)
            self.DeckCards[i],self.DeckCards[r] = self.DeckCards[r],self.DeckCards[i]

    def draw(self):
        return self.DeckCards.pop()

    def __str__(self):
        return str([card.__str__() for card in self.DeckCards])

    def __len__(self):
        return len(self.DeckCards)

class Player(object):
    def __init__(self,Name,PlayerHandcards = [],Balance = 1000):
        self.Name = Name
        self.Hand = PlayerHandcards
        self.Balance = Balance

    def deal(self,deck):
        self.Hand.append(deck.draw())

    def __str__(self):
        return 'Name :'+str(self.Name)+'\n'+'Hand: '+str([card.__str__() for card in self.Hand])+'\n'+'Balance: '+str(self.Balance)


deck1 = Deck()
deck1.builddeck()
deck1.shuffle()
Alice = Player('Alice')
Alice.deal(deck1)
print(Alice)
Bob = Player('Bob')
Bob.deal(deck1)
print(Bob)

And after dealing to Bob they both have the same hands. If you have some other suggestions regarding the code, you are welcome to share that as well.

  • 1
    Default arguments are evaluated exactly once, when your function is created. So you guessed correctly that there default argument is always the same object for all invocations. That's why it's recommended to use immutable types only for default arguments. I'll find a duplicate for you momentarily. – Mad Physicist Jul 26 '18 at 04:21
  • 2
    Possible duplicate of ["Least Astonishment" and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – Mad Physicist Jul 26 '18 at 04:23
  • Thank you so much. It has to be immutable, got it!! Thanks a ton!! Also how do I upvote? – Balakrishnan Rajan Jul 26 '18 at 04:32

1 Answers1

0

This is a duplicate of “Least Astonishment” and the Mutable Default Argument as indicated by @Mad Physicist. Closing this question for the same.