Skip to content

Instantly share code, notes, and snippets.

@Se7enSquared
Last active October 18, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Se7enSquared/d8d9e43f909dfab0f259091c645c7e56 to your computer and use it in GitHub Desktop.
Save Se7enSquared/d8d9e43f909dfab0f259091c645c7e56 to your computer and use it in GitHub Desktop.
Deck of cards example class deck of cards implementing dunder len and getitem with named tuples
import collections
Card = collections.namedtuple('Card', ['rank', 'suit'])
class FrenchDeck:
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment