In the following program, why is "Ac" the first thing that the program outputs? In main, print "Printing a Card Object" is the first think I see that should print anything.
Is it the __str__ method using the rank & suit attributes from the __init__ constructor?

# Playing Cards
# Demonstrates combining objects

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 Hand(object):
    """ A hand of playing cards. """
    def __init__(self):
        self.cards = []
        
    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        else:
            rep = "<empty>"
        return rep
    
    def clear(self):
        self.cards = []
        
    def add(self, card):
        self.cards.append(card)
        
    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)
        
# main
card1 = Card(rank = "A", suit = "c")
print "Printing a Card Object"
print card1

card2 = Card(rank = "2", suit = "c")
card3 = Card(rank = "3", suit = "c")
card4 = Card(rank = "4", suit = "c")
card5 = Card(rank = "5", suit = "c")

print "\nPrinting the rest of the objects individually:"
print card2
print card3
print card4
print card5

my_hand = Hand()
print "\nPrinting my hand before I add any cards:"
print my_hand

my_hand.add(card1)
my_hand.add(card2)
my_hand.add(card3)
my_hand.add(card4)
my_hand.add(card5)

print "\nPrinting my hand after adding 5 cards:"
print my_hand

your_hand = Hand()
my_hand.give(card1, your_hand)
my_hand.give(card2, your_hand)

print "\nGave the first two cards from my hand to your hand."
print "Your hand:"
print your_hand
print "My hand:"
print my_hand

my_hand.clear()
print "\nMy hand after clearing it:"
print my_hand

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

Here i try to break the first class to make it clearer.

Is it the __str__ method using the rank & suit attributes from the __init__ constructor?

Yes __str__ use rank and suite from __init__.
Return a human-readable string.

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):
        '''Returns pleasant string representation of the object'''
        self.rep = self.rank + self.suit
        return self.rep

#First we have to give to argument to __init__
#self as you may know dos not count as an argument(self is the class glue and transport data between methods)
card1 = Card(rank = "A", suit = "c")  #Class instance

print card1.RANKS  #class attributes we can call like this
print "Printing a Card Object"
print card1  #__str__ method at work,dont need to use card.<something>
print card1.rank  #here we access rank
#look i have put self before rep(self.rep)
#Then we can call rep like this to
print card1.rep

'''my oytput-->
['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
Printing a Card Object
Ac
A
Ac
'''

Edited 7 Years Ago by snippsat: n/a

This article has been dead for over six months. Start a new discussion instead.