class Deck:
def __init__(self):
self.cards=[]
for suit in range(4):
for rank in range(1,14):
card=Card( suit, rank )
self.cards.append(card)
def __str__ (self):
res=[]
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
def pick_card(self):
from random import shuffle
shuffle(self.cards)
return self.cards.pop()
def add_card(self,card):
if isinstance(card, Card): #check if card belongs to card Class!!
self.cards.append(card)
def move_cards(self, gen_hand, num):
for i in range(num):
gen_hand.add_card(self.pick_card())
class Hand(Deck):
def __init__(self, label=''):
self.cards = []
self.label = label
def __str__(self):
return 'The {} is composed by {}'.format(self.label, self.cards)
mazzo_uno = Decks()
hand = Hand('New Hand')
mazzo_uno.move_cards(hand, 5)
print(hand)
I'm trying to learn objected oriented programming. I have this problem when I try to print the object hand from the subclass Hand(). I got printed something like this <main.Card object at 0x10bd9f978> instead of the proper string name of the 5 cards in self.cards
list :
The New Hand is composed by [<__main__.Card object at 0x10bd9f978>,
<__main__.Card object at 0x10bd9fd30>, <__main__.Card object at 0x10bd9fe80>,
<__main__.Card object at 0x10bcce0b8>, <__main__.Card object at 0x10bd9fac8>]
I tried also to do this to transform self.cards in string but I get "TypeError: sequence item 0: expected str instance, Card found"
.
def __str__(self):
hand_tostr = ', '.join(self.cards)
return 'The {} is composed by {}'.format(self.label, hand_tostr)
I read on other answers on this site that I should use __repr__
but I didn't understand how to add it in Hand class.