比较实例与__eq__为什么不起作用?

时间:2017-01-03 23:48:09

标签: python tkinter

我无法弄清楚函数createDeck无效的原因。

class Card:
    def __init__(self):
        self.rank = tk.IntVar()
        self.rank.set(rm.choice(rank))
        self.seed = tk.StringVar()
        self.seed.set(rm.choice(seeds))
        self.point = tk.IntVar()
        self.point.set(self.calculatePoint())
        self.value = tk.StringVar()
        self.value.set(self.calculateValue())
        self.name = tk.StringVar()
        self.name.set(self.calculateName())

    def __str__(self):
        return self.name.get() + ' di ' + self.seed.get()

    def __lt__(self, other):
        return self.rank.get() < other.rank.get()

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        return self.rank.get() != other.rank.get() and self.seed.get() != other.seed.get()

    def __gt__(self, other):
        return self.rank.get() > other.rank.get()

class Deck:
    def __init__(self):
        self.deck= []
        self.name = tk.Label(table, text='Deck', bd=3, relief='flat')
        self.name.grid(column=0, row=0)

    def createDeck(self):
        while len(self.deck) < 40:
            x = Card()
            if x not in self.deck:
                deck.append(x)
        table.create_text(500,200, text='deck is compose by %d cards' % (len(self.deck)))

1 个答案:

答案 0 :(得分:0)

你的方法:

def createDeck(self):
    while len(self.deck) < 40:
        x = Card()
        if x not in self.deck:
            deck.append(x)

有一个未定义的变量deck

将其更改为self.deck

def createDeck(self):
    while len(self.deck) < 40:
        x = Card()
        if x not in self.deck:
            self.deck.append(x)

应该会有帮助。