Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81935

Re: Can you use self in __str__

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!81.171.88.16.MISMATCH!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=4081e7d36=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.065
X-Spam-Evidence '*H*': 0.87; '*S*': 0.00; 'broken': 0.04; 'attribute': 0.07; 'class,': 0.07; 'problem:': 0.07; 'friday,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '2.7': 0.14; "%s'": 0.16; "'%s": 0.16; 'cards.': 0.16; 'container.': 0.16; 'etc...': 0.16; 'name):': 0.16; 'players:': 0.16; 'url:py': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'thu,': 0.19; 'not,': 0.20; 'starts': 0.20; 'command': 0.22; 'handles': 0.22; 'issue.': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'player': 0.26; 'this:': 0.26; 'pass': 0.26; 'post': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'skip:( 20': 0.30; 'url:mailman': 0.30; 'included': 0.31; 'code': 0.31; 'posting': 0.31; 'container': 0.31; 'lot.': 0.31; 'class': 0.32; 'probably': 0.32; 'run': 0.32; 'url:python': 0.33; '-----': 0.33; 'implemented': 0.33; 'skip:_ 10': 0.34; 'problem': 0.35; 'knows': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'too': 0.37; 'list': 0.37; 'email addr:python.org': 0.37; 'being': 0.38; 'thank': 0.38; 'nov': 0.38; 'skip:[ 10': 0.38; 'issue': 0.38; 'does': 0.39; 'subject:': 0.39; 'enough': 0.39; 'url:mail': 0.40; 'how': 0.40; 'future': 0.60; 'dave': 0.60; 'subject:Can': 0.60; 'full': 0.61; 'new': 0.61; 'entire': 0.61; 'you.': 0.62; 'information': 0.63; 'name': 0.63; 're:': 0.63; 'design,': 0.64; 'received:194': 0.64; 'email name :python-list': 0.65; 'here': 0.66; 'between': 0.67; 'notice:': 0.67; 'details,': 0.68; 'content,': 0.68; 'feeling': 0.68; 'person,': 0.68; 'privileged.': 0.69; 'money': 0.72; 'disclose': 0.74; 'special': 0.74; 'hand': 0.80; 'dealer': 0.84; 'dealer,': 0.84; 'etc,': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'angel': 0.91; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="5.07,517,1413237600"; d="scan'208";a="3689312"
Date Thu, 4 Dec 2014 20:22:11 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Seymore4Head <Seymore4Head@Hotmail.invalid>
In-Reply-To <4gpf7ahkq3isr8g7l6cs8dt0anu9oiva57@4ax.com>
Subject Re: Can you use self in __str__
MIME-Version 1.0
X-Mailer Zimbra 7.2.7_GA_2942 (ZimbraWebClient - GC37 (Linux)/7.2.7_GA_2942)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16578.1417720935.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417720935 news.xs4all.nl 2893 [2001:888:2000:d::a6]:44343
X-Complaints-To abuse@xs4all.nl
X-Received-Body-CRC 909370515
X-Received-Bytes 8002
Xref csiph.com comp.lang.python:81935

Show key headers only | View raw


----- 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 <davea@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.

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Can you use self in __str__ Jean-Michel Pichavant <jeanmichel@sequans.com> - 2014-12-04 20:22 +0100
  Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-12-04 14:52 -0500

csiph-web