如何将类对象追加到列表中

时间:2015-08-20 20:19:46

标签: python list class object append

在实例化一个套牌(deck = Deck())之后,调用deck.show_deck()只打印出两个钻石" 52次。 '复制'部分符合this answer,但似乎没有帮助。有什么建议吗?

import copy
from card import Card

class Deck:

    card_ranks = ['ace','king','queen','jack','ten','nine','eight','seven','six','five','four','three','two']
    card_suites = ['clubs','hearts','spades','diamonds']

    deck = []   

    def __init__(self):
        #create a deck of 52 cards

        for suite in Deck.card_suites:
            for rank in Deck.card_ranks:
                Deck.deck.append(copy.deepcopy(Card(card_rank=rank, card_suite=suite)))



    def show_deck(self):
        for item in Deck.deck:
            print item.get_name()

卡:

class Card:

    card_name = ''

    def __init__(self, card_rank, card_suite):
        self.card_rank = card_rank.lower()
        self.card_suite = card_suite.lower()
        Card.card_name = card_rank + " of " + card_suite

    def get_name(self):
        return Card.card_name

2 个答案:

答案 0 :(得分:4)

这里的问题是Card类有一个名称变量,它与Card类的所有实例共享。

当你有:

class Card:

    card_name = ''

这意味着所有Card个对象都具有相同的名称(card_name),这几乎肯定不是您想要的。

您必须使名称成为实例的一部分,如下所示:

class Card:
    def __init__(self, card_rank, card_suite):
        self.card_rank = card_rank.lower()
        self.card_suite = card_suite.lower()
        self.card_name = card_rank + " of " + card_suite

    def get_name(self):
        return self.card_name

您会发现deepcopy不是必需的,也不需要它,但它确实向您显示deepcopy不允许您保留不同类变量的状态。

此外,如果您想将其打印出来,我建议您更改Card以拥有自己的__str__方法:

class Card:
    def __init__(self, card_rank, card_suite):
        self.card_rank = card_rank.lower()
        self.card_suite = card_suite.lower()

    def __str__(self):
        return "{0} of {1}".format(card_rank, card_suit)

这使用Python语言本身来打印类,并且具有以下优势:您的类现在可以在print语句和转换为字符串时正常工作。所以而不是:

print some_card.get_name()

你可以做到

print some_card

答案 1 :(得分:2)

扩展了shuttle87所说的内容:

$(document).ready(function(){
  $("#slider>img>#1").fadeIn(3000);
});

使class Card: card_name = '' 成为静态变量(在该类的所有实例之间共享)

将变量设置为非静态变量(在card_name方法中使用self.card_name)后,您不必担心复制部分,因为卡类的每个实例都拥有它自己的唯一名称

在该注释中,Deck中的__init__在您的代码中也是静态的。

deck
from card import Card

class Deck:
    # these 2 can be static, they never change between copies of the deck class
    card_ranks = ['ace','king','queen','jack','ten','nine','eight','seven','six','five','four','three','two']
    card_suites = ['clubs','hearts','spades','diamonds']

    def __init__(self):
        # this shouldn't be static since you might want to shuffle them 
        # or do other things that make them unique for each deck
        self.cards = []

        for suite in Deck.card_suites:
            for rank in Deck.card_ranks:
                self.cards.append(Card(rank, suite))

    def show_deck(self):
        for item in self.cards:
            print item
class Card:
    def __init__(self, rank, suite):
        self.rank = rank
        self.suite = suite

    def __str__(self):
        return self.rank + ' of ' + self.suite
#! python2

from deck import Deck

def main():
    deck = Deck()

    deck.show_deck()

if __name__ == '__main__':
    main()