Python 无法使用类的实例打印类列表属性
我正在设计一个deck类,它有init()方法,并且最初有一个空列表。然后我把我的卡片添加到列表中。我正在尝试创建一个x实例并访问这副牌的洗牌版本。我知道已经发布了很多解决方案。我只是想用我的逻辑来理解,我能够打印卡片元素的地址,而不是卡片组本身。在尝试调试时,我不知道print(x.cards_in_deck)是在打印位置还是在打印x.shuffle。对于Pycharm调试的任何好的参考资料,我们也将不胜感激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', '
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)
方法。如果不指定“特殊”repr和str,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)
方法。如果不指定“特殊”repr和str,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进行调试…)