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

Python 无法使用类的实例打印类列表属性,python,python-3.x,list,class,printing,Python,Python 3.x,List,Class,Printing,我正在设计一个deck类,它有init()方法,并且最初有一个空列表。然后我把我的卡片添加到列表中。我正在尝试创建一个x实例并访问这副牌的洗牌版本。我知道已经发布了很多解决方案。我只是想用我的逻辑来理解,我能够打印卡片元素的地址,而不是卡片组本身。在尝试调试时,我不知道print(x.cards_in_deck)是在打印位置还是在打印x.shuffle。对于Pycharm调试的任何好的参考资料,我们也将不胜感激 suits = ('Hearts', 'Diamonds', 'Spades', '

我正在设计一个deck类,它有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')

第一个print语句将在deck对象上打印实例变量“cards\u in\u deck”的值。变量的类型是一个列表,因此在打印时,它将如下所示:

[Two of Hearts, Three of Hearts, <more items>, King of Clubs, Ace of Clubs]
这将在每一行上打印每张卡的名字


编辑:我没有看到您在卡片类中使用了“str”和“not”repr。在打印列表时,Python使用_repr___________________________________。请参阅:

第一个print语句将在deck对象上打印实例变量“cards\u in\u deck”的值。变量的类型是一个列表,因此在打印时,它将如下所示:

[Two of Hearts, Three of Hearts, <more items>, King of Clubs, Ace of Clubs]
这将在每一行上打印每张卡的名字


编辑:我没有看到您在卡片类中使用了“str”和“not”repr。在打印列表时,Python使用_repr___________________________________。请参阅:

如果打印
列表
,则
列表
中的任何项目都将使用其
repr()
进行打印。您可以通过指定类的
\uuuuu repr(self)\uuuuu
方法来定义所使用的内容-您可能还需要定义类的
\uuuuu str\uuuuuu(self)
方法。如果不指定“特殊”reprstr,python将为您创建默认值,这些值可能不会打印您想要的内容

见:

两者都应该返回一个字符串-您正在而不是做什么,您将为您的
Deck
类返回一个列表,该列表违反了这些方法的约定:

Doku:

两种描述都说:

返回值必须是字符串对象

并提供更多关于它们的信息


修正:

输出:

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
调试:

  • (或使用带有内置支持的IDE进行调试…)

如果打印
列表
列表
中的任何项目都将使用其
repr()
进行打印。您可以通过指定类的
\uuuuu repr(self)\uuuuu
方法来定义所使用的内容-您可能还需要定义类的
\uuuuu str\uuuuuu(self)
方法。如果不指定“特殊”reprstr,python将为您创建默认值,这些值可能不会打印您想要的内容

见:

两者都应该返回一个字符串-您正在而不是做什么,您将为您的
Deck
类返回一个列表,该列表违反了这些方法的约定:

Doku:

两种描述都说:

返回值必须是字符串对象

并提供更多关于它们的信息


修正:

输出:

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
调试:

  • (或使用带有内置支持的IDE进行调试…)

先生,我无法打印列表。它只是打印列表项的位置。我得到了这种类型的输出…[,是否有类似于c中的指针方法来捕获该地址并打印值,因为如果您所说的是真的,那么我总是需要添加u str_uuuuuuuuuuuuuuuuuuuu()或u repr_uuuuuuuu()类的方法!!!是否总是需要打印_ustr___()和_repr__()方法返回字符串类型?如果不需要,那么我们可以创建自己的函数转换返回类型并作为函数(值)打印。为什么它们被特别称为?请原谅,如果你觉得我很傻,我是个新手!!!你可能想错了。它仍然在打印对象,而不是地址。但是,如果类上没有定义uu repr u,它不知道应该用文本来表示该对象。所以它只会打印它知道的东西:它在该地址是一个卡片对象。如果你是maki如果你想用print调用一个Python类,那么你应该在它上面实现_repr__。先生,我无法打印列表。它只是打印列表项的位置。我正在得到这种类型的输出。。。[,是否有任何像c中那样的指针方法来捕获该地址并打印值,因为如果您所说的是真的,那么我总是需要向类添加_str___()或_repr_____()方法!!!是否总是需要__str_____()和__repr______方法返回要打印的字符串类型?如果没有必要,那么我们可以创建自己的函数转换返回类型并作为函数(值)打印。为什么它们被专门称为_str___()和_repr______41;?请原谅,如果你觉得我很傻,我是个新手!!!你可能想错了。它仍然在打印对象,而不是地址。但是,如果类上没有定义uu repr u,它不知道应该用文本来表示该对象。所以它只会打印它知道的东西:它在该地址是一个卡片对象。如果你是maki如果您认为将使用print调用一个Python类,那么您应该在它上面实现_repr__。