On Thu, 4 Dec 2014 20:22:11 +0100 (CET), Jean-Michel Pichavant
<jeanmic...@sequans.com> wrote:

>----- Original Message -----
>> From: "Seymore4Head" <Seymore4Head@Hotmail.invalid>
>> To: python-list@python.org
>> Sent: Friday, 28 November, 2014 4:31:50 AM
>> Subject: Re: Can you use self in __str__
>> 
>> On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel <da...@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__.
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
>I think your main problem is a design issue.
>
>I won't go into details, it would be too long but here's a way to fix your 
>problem:
>
># Untested code
>
>class Player(object):
>  def __init__(self, name):
>    self.name = name
>    self.hand = Hand()
>  
>  def __str__(self):
>    return '%s<%s> has %s' % (self.__class__.__name__, self.name, self.hand)
>
># A dealer is a special type of player
>class Dealer(Player): pass
>
>def deal():
>    global outcome, in_play,deck,dealer,player
>    
>    # your code goes here
>    deck=Deck()
>    deck.shuffle()
>    print deck
>    players = [Dealer('Robert'), Player('John')]
>
>    for deal in range(2):
>      for player in players:
>        player.hand.add_card(deck.deal_card())
>
>    for player in players:
>      print str(player)
>    in_play = True
>
>
>With the above design, the relation between player and its hand is implemented 
>with the Player attribute "hand".
>It's a classic design where the container knows about the content, but the 
>content does not know its container.
>
>There are other solutions which do not require a new class, but I have the 
>feeling you will need the Player class in the future.
>
>Moreover The design above I gave you will be probably broken in the future 
>when you add features to your code.
>
>The same player may be a dealer or not, it may vary over time. You will 
>probably need a 'Table' object which handles a collection of players, with a 
>dealer, small blind, big blind etc, amount of money in the pot etc...
>
>JM
>
>
>-- IMPORTANT NOTICE: 
>
>The contents of this email and any attachments are confidential and may also 
>be privileged. If you are not the intended recipient, please notify the sender 
>immediately and do not disclose the contents to any other person, use it for 
>any purpose, or store or copy the information in any medium. Thank you.

Thanks for the tips
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to