![](/img/trans.png)
[英]{“title”:“Unsupported Media Type”,Error for no matter what i try :I(
[英]No matter what I try I cant print the value I keep getting it <__main__.card object at -->
我正在嘗試將卡片 c 添加到卡片組 d,但我無法打印卡片組,而是不斷得到<__main__.card at >
class Card():
def __init__(self,theName,theMoves):
self.name = theName
self.moves = list(theMoves)
class Deck():
theCards = []
def __init__(self):
pass
def addCard(self,theCard):
Deck.theCards.append(theCard)
print(Deck.theCards)
#adding card to my deck
c = Card("sh",["fire",258])
d = Deck()
d.addCard(c)
您不是指 class 函數中的特定 class 實例。 更改您的Deck
class 定義如下:
class Deck():
def __init__(self):
self.theCards = []
def addCard(self, theCard):
self.theCards.append(theCard)
print(self.theCards)
您應該遍歷您的卡片並在其上調用 vars(),因為打印對象列表只會顯示某些 memory 地址,但沒有屬性。 代替:
print(Deck.theCards)
使用與此類似的循環:
for card in Deck.theCards:
print(vars(card))
它打印 object 的所有變量及其對應值。 我還假設您希望擁有不同玩家的不同套牌,但您的代碼當前將卡片列表保存在 class 定義中。 因此,您使用 Deck() 調用創建的所有 Deck 類型的對象都共享相同的卡片列表。 假設您添加了另一個套牌:
#adding card to my deck
print("player 1")
c = Card("sh",["fire",258])
d = Deck()
d.addCard(c)
print("player 2")
c2 = Card("it",["water",258])
d2 = Deck()
d2.addCard(c2)
玩家 2 的牌組還包括玩家 1 的牌,反之亦然。 建議代碼擴展的打印 output:
player 1
{'name': 'sh', 'moves': ['fire', 258]}
player 2
{'name': 'sh', 'moves': ['fire', 258]}
{'name': 'it', 'moves': ['water', 258]}
用戶“插入隨機”之前已經提供了解決方案。 要獲得具有兩個不同卡片列表的兩個不同的牌組,您必須將卡片列表存儲在 class 實例中,而不是 class 定義中。 這里是整個更新的代碼:
class Card():
def __init__(self,theName,theMoves):
self.name = theName
self.moves = list(theMoves)
class Deck():
def __init__(self):
self.theCards = []
def addCard(self,theCard):
self.theCards.append(theCard)
for card in self.theCards:
print(vars(card))
#adding card to my deck
print("player 1")
c = Card("sh",["fire",258])
d = Deck()
d.addCard(c)
print("player 2")
c2 = Card("it",["water",258])
d2 = Deck()
d2.addCard(c2)
請注意,使用 self 而不是 Deck 為使用 Deck() 調用創建的每個新牌組存儲不同的卡片列表。 此列表由初始 class 構造函數 function 而非 class 定義本身創建。
控制台 output:
player 1
{'name': 'sh', 'moves': ['fire', 258]}
player 2
{'name': 'it', 'moves': ['water', 258]}
我猜你想要什么。
帶字典的版本:
#adding card to my deck
print("player 1")
c = {'name': 'sh', 'moves':["fire",258] }
d = { 'theCards': [c] }
print(list(card for card in d['theCards']))
print("player 2")
c2 = {'name': 'it', 'moves':["water",258] }
d2 = { 'theCards': [c2] }
print(list(card for card in d2['theCards']))
默認情況下,當 Python 打印列表時,它會打印列表元素的repr 。 對於許多對象,包括自定義類,默認的repr
看起來像
<__main__.MyClass object at 0x7f00ff75fd00>
您可以通過在類上定義__repr__
方法來覆蓋此行為。 按照慣例, __repr__ 的__repr__
應該是一個字符串,如果它被傳遞給eval ,它將產生你的 object 。
對於卡 class,它看起來像這樣:
class Card:
def __init__(self, theName, theMoves):
self.name = theName
self.moves = list(theMoves)
def __repr__(self):
return "Card('{}', {})".format(self.name, self.moves)
將此 output 提供給您的腳本(一旦您修復了縮進):
[Card('sh', ['fire', 258])]
您還可以定義一個__str__
方法來自定義 object 在調用str(myobject)
時的顯示方式。 如果__str__
未定義 Python 將使用__repr__
代替。
你似乎有一個縮進錯誤。 您對c
、 d
和d.addCard(c)
的聲明應該縮進一次。 對於您的Card
和Deck
組 class 中的所有內容,go 也同樣需要。
class Card():
def __init__(self,theName,theMoves):
self.name = theName
self.moves = list(theMoves)
class Deck():
theCards = []
def __init__(self):
pass
def addCard(self,theCard):
Deck.theCards.append(theCard)
print(Deck.theCards)
#adding card to my deck
c = Card("sh",["fire",258])
d = Deck()
d.addCard(c)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.