In the program below, I have two questions. First, the first object, card1 passes the strings A & c to it's master class. What I want to know is, what do these two strings have to do with the A & c in RANKS & SUITS? If the class had been based the number of the element in RANKS & SUITS I wouldn't be confused right now but I don't see RANKS or SUITS being used.

Secondly, why is there an __init__ method inside the Positionable_Card class with rank and suit, but is then instructed to use rank and suit from it's base class? Thanks for any and all replies.

# Playing Cards 3.0
# Demonstrates inheritance - overriding methods

class Card(object):
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    
    SUITS = ["c", "d", "h", "s"]
    
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit
        
    def __str__(self):
        rep = self.rank + self.suit
        return rep
 

class   Unprintable_Card(Card):
        """A Card that won't reveal it's ran or suit when printed. """
        def __str__(self):
            return "<unprintable>"
        
        
class Positionable_Card(Card):
    """ A Card that can be face up or face down. """
    def __init__(self, rank, suit, face_up = True):
        super(Positionable_Card, self).__init__(rank, suit)
        self.is_face_up = face_up
        
    def __str__(self):
        if self.is_face_up:
            rep = super(Positionable_Card, self).__str__()
        else:
            rep = "XX"
        return rep

    def flip(self):
        self.is_face_up = not self.is_face_up
        
# main
card1 = Card("A", "c")
card2 = Unprintable_Card("A", "d")
card3 = Positionable_Card("A", "h")

print "Printing a Card object:"
print card1

print "\nPrinting an Unprintable_Card object:"
print card2

print "\nPrinting a Positionable_Card object:"
print card3

print "Flipping the Positionable_Card object."
card3.flip()

print "Printing the Positionable_Card object:"
print card3

raw_input("\n\nPress the enter key to exit.")

Recommended Answers

All 3 Replies

Well, the two RANKS and SUITS part of the Card class are unnecessary I believe.
The super allows the Positionable_Card class to refer to its parent, the Card class without naming the Card class explicitly.
That way, a Positionable_Card object will have a rank and suit just like a Card object would too.

Secondly, why is there an __init__ method inside the Positionable_Card class with rank and suit, but is then instructed to use rank and suit from it's base class? Thanks for any and all replies.

Take it out and see what happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.