Nov-17-2016, 11:32 PM
I Wasn't too sure if I should add two cents or not.
This is for illustration purposes of other ways to create a deck:
Fluent Python is one of my favorite python books:
This is for illustration purposes of other ways to create a deck:
Fluent Python is one of my favorite python books:
# attribution: # “Fluent Python by Luciano Ramalho (O’Reilly). # Copyright 2015 Luciano Ramalho, 978-1-491-94600-8. import collections from random import shuffle Card = collections.namedtuple('Card', ['rank', 'suit']) class FrenchDeck(collections.MutableSequence): ranks = [str(n) for n in range(2, 11)] + list('JQKA') suits = 'spades diamonds clubs hearts'.split() def __init__(self): self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks] def __len__(self): return len(self._cards) def __getitem__(self, position): return self._cards[position] def __setitem__(self, position, value): # <1> self._cards[position] = value def __delitem__(self, position): # <2> del self._cards[position] def insert(self, position, value): # <3> self._cards.insert(position, value) def main(): deck = FrenchDeck() shuffle(deck) for card in deck: print(card) if __name__ == '__main__': main()