在python的OOP中,使用默认值初始化时对象的不同实例是否相同?

时间:2018-07-26 04:04:29

标签: python-3.x oop

我试图了解面向对象的编程。我通过创建一个像扑克一样的小程序来做到这一点。我遇到了一个问题,它的最小工作示例是:

对于此代码:

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)

我希望这个输出:

Name: Aboy
List: [44] 

(介于1到50之间的数字)

Name: Anotherboy
List: [11] 

(还是1到50之间的随机数) 但是我得到的是:

Name: Aboy
List: [44] 

(符合我的期望)

Name: Anotherboy
List: [44,11] 

(将这个数字附加到前一个实例的列表中) 为什么会这样呢?上下文是从牌组中发给两名玩家一张牌。很抱歉,如果存在类似的问题,如果确实存在,请您指出来,我会仔细阅读。堆栈溢出新手。预先感谢。

对于非最小示例,我正在尝试:

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)

在和鲍勃打交道之后,他们俩都拥有同一个牌。如果您对代码有其他建议,也欢迎您分享。

1 个答案:

答案 0 :(得分:0)

这是“疯狂惊讶”和@Mad Physicist指示的可变默认参数的重复。以同样的方式结束这个问题。