[Edu-sig] more fun with cards

kirby urner kirby.urner at gmail.com
Thu Oct 8 01:46:22 CEST 2009

The code below isn't stellar, but has some pedagogical value nevertheless.

In general, I'm looking at constituting a deck of 52 cards, or 54 with
two jokers, this time with each card an object.

In a C struct, you could imagine the suit (e.g. Diamond), rank (e.g.
Jack) and value (e.g. 11) all having their own slots, which is pretty
much what we're doing here.

A Deck object is a list of cards, not a dictionary, as sequence
matters.  Shuffling is automatic upon deck creation, which somewhat
deviates from brick and mortar cards, but oh well.

Other features:  use of break keyword to escape from a loop in a loop,
difference twixt __str__ and __repr__ ribs.

I'd call this intermediate Python in that we're assuming fluency with
modules like random and native functions like zip, also show off a
privacy feature (using _ and/or __ as a method prefix).

Use of decorators might be a next topic, starting with @property.

As I've been discussing off list, I'm vested in going with cards,
dice, games of chance often associated with gambling.

I am well aware that a home schooling minority don't want their kids
exposed to a shady underworld associated with Prohibition and Chicago

I suggest Ruby might be better for those households.

When I mailed PSF snake back to Virginia yesterday, the UPS clerk
shuddered in horror, didn't want to look in the satchel even when I
told her what was in it, a quite harmless stuffed totem (she was in
Portland for DjangoCon, to meet Django Pony).

The baby Adonis (by now a lanky lad given how snake years fly by) is
staying behind in Portland to learn more from our team.  He's
currently studying the J language, having semi-mastered his native

(One Laptop per Snake -- OLPS).



from random import shuffle

thesuits = ['Hearts','Diamonds','Clubs','Spades']
theranks = ['Ace'] + [str(v) for v in range(2,11)] + ['Jack','Queen','King']
thevalues = zip(theranks, range(1,14))

class Card:
    def __init__(self, suit, (rank, value)):
        self.suit = suit
        self.rank = rank
        self.value = value

    def __repr__(self):
        return "Card(%s, %s)" % (self.suit, (self.rank, self.value))

    def __str__(self):
        return "%s of %s" % (self.rank, self.suit)

class Deck:

    def __init__(self, numcards = 52):
        self.numcards = numcards
        self.cards = []

    def _populate(self):
        have = 0
        self.cards = []
        for suit in thesuits:

            for value in thevalues:
                self.cards.append( Card(suit, value))
                have += 1
                if have == self.numcards:

            if have == self.numcards:

    def __repr__(self):
        return "Deck(%s)" % self.numcards

    def __str__(self):
        return str([str(card) for card in self.cards])

def test():
    thedeck = Deck()
    print str(thedeck)

if __name__ == '__main__':

More information about the Edu-sig mailing list