How do I create a special method __repr__ where I can print, for example, '6 of spades' or 'Q of diamonds'?
How do I access the data from the namedtuple, keeping in mind that I have a list of namedtuples in self._cards?
import collections
cards = collections.namedtuple('Card', ['rank', 'suit'])
class Deck:
ranks = [str(n) for n in range (2,11)] + list('JQKA')
suits = 'spades diamonds hearts clubs'.split()
def __init__(self):
self._cards = [cards(rank, suit) for suit in self.suits for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, item):
return self._cards[item]
def __repr__(self):
return '%s of %s' % () # <-- don't know how to assign the string
b = ()
for i in b:
print(i)