[Tutor] Shut up and deal!

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Aug 8 20:45:51 CEST 2005



On Mon, 8 Aug 2005, Greg Lindstrom wrote:

> I found the conversation of dealing cards interesting, so I took a few
> minutes (about 20 while I waited for a production run to finish) and
> came up with the following Dealer class.

[text cut]

> What type of Pythonic changes would make this? What features would be
> nice?


Hi Greg,

For your program, it can make sense to treat the Cards themselves as a
class, rather than have them be just a string representation of a card.
Something like this:

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

might work because then we can treat Cards as a separate data type.

One advantage is that we can then later extract the rank or suit out of
the card without having to do any string manipulation.  If we still want
to make it easy to get a string representation of these cards, we can add
an __str__() method to the Card class definition.

There's often a temptation to make everything a string because we're
comfortable with them, but sometimes that representation choice makes
things hard on ourselves when we're doing more than pure string
manipulation.

Hope this helps!



More information about the Tutor mailing list