Can you use self in __str__

Seymore4Head Seymore4Head at Hotmail.invalid
Fri Nov 28 04:31:50 CET 2014


On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel <davea at davea.name>
wrote:

class Hand:
    def __init__(self):
        self.hand = []
        # create Hand object

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

I am using 2.7 (Codeskulptor).  This is working code.  It starts with
an empty list that gets appended from a full deck of shuffled cards.
dealer=Hand()
player=Hand()
I don't really know how to post working code without posting a lot.  I
am not being too successful in trying to post enough code to have it
work without posting the entire code.
Here is the link if you want to run it.
http://www.codeskulptor.org/#user38_Kka7mh2v9u_9.py
The print out looks like this:
Hand contains H4 DQ.

I can (and am) currently printing the hand like this:
print "Player's",player
print "Dealer's",dealer

My question is can you add (self) in the __str__ so when you issue the
command "print player" the "player" part is included in the __str__.



More information about the Python-list mailing list