Can you use self in __str__

Rustom Mody rustompmody at gmail.com
Fri Nov 28 09:21:26 EST 2014


On Friday, November 28, 2014 6:57:23 AM UTC+5:30, Seymore4Head wrote:
> 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

And what do you think should be printed if instead of your code

dealer = Hand()
player = Hand()

we have the following

dealer = Hand()
player = dealer

??



More information about the Python-list mailing list