Can you use self in __str__

Seymore4Head Seymore4Head at Hotmail.invalid
Thu Nov 27 20:26:45 EST 2014


    def __str__(self):
        s = "Hand contains "
        for x in self.hand:
            s = s + str(x) + " "
        return s

This is part of a Hand class.  I need a hand for the dealer and a hand
for the player.
dealer=Hand()
player=Hand()
This prints out 'Hand contains " foo bar 
for both the dealer's hand and the player's hand.

Is there a way to include "self" in the __string__ so it reads
Dealer hand contains foo bar
Player hand contains foo bar

I tried:
s  = self + "Hand contains "  and that doesn't work.
s = self + s + str(x) + " "  doesn't work either
Line 60: TypeError: cannot concatenate 'str' and 'Hand' objects

I tried:
s = self.hand + s + str(x) + " "
Line 60: TypeError: can only concatenate list to list



More information about the Python-list mailing list