无法使用类的实例打印类列表属性

时间:2019-01-01 00:02:38

标签: python python-3.x list class printing

我正在设计一个具有 init ()方法的牌组类,该类最初具有一个空列表。然后,我将卡片添加到列表中。我正在尝试创建一个x实例,并访问纸牌组的改组版本。我知道已经发布了许多解决方案。我只是想用我的逻辑理解我能够打印卡元素的地址,而不是卡座本身。在尝试调试时,我不知道print(x.cards_in_deck)是打印位置还是x.shuffle...。任何对Pycharm调试的良好参考也将受到高度赞赏。

suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,
         'Queen':10, 'King':10, 'Ace':11}
class Card:

    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank


    def __str__(self):
        return self.rank +' of '+self.suit
class Deck:

    def __init__(self):

        self.cards_in_deck = []
        for suit in suits:
            for rank in ranks:
                self.cards_in_deck.append(Card(suit, rank))
        #return self.cards_in_deck

    def __str__(self):
        # for card in Deck.cards_in_deck:
        #     return(card)
        return self.cards_in_deck

    def shuffle_cards(self):
        return random.shuffle(self.cards_in_deck)

x = Deck()
print(x.cards_in_deck,sep ='\n')
print(x.shuffle_cards(),sep = '\n')

2 个答案:

答案 0 :(得分:2)

第一个打印语句将在Deck对象上打印实例变量“ cards_in_deck”的值。变量的类型是一个列表,因此在打印时,它看起来像:

[Two of Hearts, Three of Hearts, <more items>, King of Clubs, Ace of Clubs]

第二个print语句将调用Deck对象上的shuffle_cards方法,并打印该方法返回的内容。该方法返回在列表上调用random.shuffle的结果。结果是一个新列表,那就是洗过的卡片。

现在,这些打印语句中的每一个都在打印列表对象,即上面的文本所示。如果要打印每张卡片,则需要遍历卡片,类似于初始化卡座时遍历列表的方式。因此,您可以执行以下操作:

for card in x.cards_in_deck:
    print(card)

这会将每张卡的名称打印在新行上。

编辑:我没有看到您在Card类中使用的是__str__而不是__repr__。在打印列表时,Python使用__repr__方法来确定要放置在列表中的位置。参见:Confused about __str__ on list in Python

答案 1 :(得分:2)

如果您打印list,则list中的任何项目都将使用repr()打印。您可以通过指定类的__repr(self)__方法来定义使用的内容-您可能还需要定义类的__str__(self)方法。如果您未指定“特殊” repr str ,则python将为您创建默认代码,这些默认代码可能不会打印您想要的

请参阅:Difference between __str__ and __repr__?

两者都应返回一个字符串-您正在做 不是 ,您将返回Deck类的列表,这违反了这些方法的约定:< / p>

Doku:

两个描述都说:

  

返回值必须是一个字符串对象。

并提供有关它们的更多信息。


修复:

suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,
         'Queen':10, 'King':10, 'Ace':11}

import random

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

    def __str__(self):
        return self.rank +' of '+self.suit
    def __repr__(self):
        return self.rank +' of '+self.suit

class Deck:
    def __init__(self):
        # always create shuffled decks
        self.create_new_deck()

    def __str__(self):  
        # return a string here, not a list
        # join only takes strings so you need to stringify each card beforehand
        return ', '.join(str(c) for c in self.cards_in_deck)        

    def __repr__(self):
        return ', '.join(str(c) for c in self.cards_in_deck)

    # create a new deck and shuffle it immediately
    def create_new_deck(self):
        self.cards_in_deck = [Card(s, r) for s in suits for r in ranks]
        random.shuffle(self.cards_in_deck)

x = Deck()
print(str(x)) 

输出:

King of Clubs, Ace of Diamonds, Seven of Spades, Ace of Spades, Three of Hearts, 
Eight of Hearts, Five of Clubs, Four of Spades, King of Diamonds, Five of Hearts, 
Eight of Spades, Three of Diamonds, Three of Spades, Six of Diamonds, 
Eight of Diamonds, Queen of Hearts, Ace of Hearts, Ten of Clubs, Two of Diamonds, 
Six of Clubs, King of Hearts, Seven of Clubs, Queen of Clubs, King of Spades, 
Nine of Diamonds, Six of Hearts, Nine of Clubs, Queen of Diamonds, Queen of Spades,
 Ten of Diamonds, Seven of Hearts, Ten of Hearts, Eight of Clubs, Ace of Clubs,
Jack of Clubs, Nine of Spades, Four of Diamonds, Seven of Diamonds, Nine of Hearts,
 Two of Clubs, Jack of Hearts, Jack of Spades, Jack of Diamonds, Two of Spades, 
Ten of Spades, Four of Hearts, Three of Clubs, Six of Spades, Five of Spades, 
Two of Hearts, Five of Diamonds, Four of Clubs

调试: