Return-Path: 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; 'method.': 0.07; 'method,': 0.09; 'def': 0.12; '2.7': 0.14; "wouldn't": 0.14; 'attribute.': 0.16; 'cards.': 0.16; 'guess.': 0.16; 'url:py': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'thu,': 0.19; 'starts': 0.20; 'command': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'question': 0.24; 'shown': 0.26; 'suggested': 0.26; 'this:': 0.26; 'post': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'along': 0.30; 'included': 0.31; 'code': 0.31; 'posting': 0.31; 'lot.': 0.31; 'steven': 0.31; 'class': 0.32; 'run': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'but': 0.35; 'add': 0.35; 'really': 0.36; 'url:org': 0.36; 'too': 0.37; 'list': 0.37; 'being': 0.38; 'nov': 0.38; 'to:addr:python- list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'dave': 0.60; 'subject:Can': 0.60; 'full': 0.61; 'entire': 0.61; 'first': 0.61; "you've": 0.63; 'name': 0.63; 'more': 0.64; 'here': 0.66; 'received:74.208': 0.68; 'hand': 0.80; 'subject:self': 0.84; 'subject:you': 0.87; 'angel': 0.91; 'birth': 0.91 Date: Fri, 28 Nov 2014 04:37:36 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Can you use self in __str__ References: <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> <4gpf7ahkq3isr8g7l6cs8dt0anu9oiva57@4ax.com> In-Reply-To: <4gpf7ahkq3isr8g7l6cs8dt0anu9oiva57@4ax.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:se/S3Hjym9FqVDsFIE2M0cK7TqFSqHhjY7ZKzF1MKds 0e96HrXsTcfzXvtO54onYVxh9FmFcVc888628HpMHzzc4OoHO3 0T1Ox5M71bZLNmKeEDlNMDacJSbt3Co+7pHPsDe7KHHkewfCV6 0VJpUGOcGfiSq5dhH7sxe1sqJ8ZwcXYehFMrgTeSvMyPuVtLj9 +d8WBDxn2GKhy8oBjlpOB7w9k3mVNy7ndLS4WrualDpBfJa77n CBj80mRsLji5FqAOnWRiKxAwY3jHvnGTsAAJBni4EfBXzoISjI WhFefQruYc8GXaoCZ/f6ATnk9xD4NQCLIeOkEeFy34g5Dtnc1R VKK5bvFKUwNtOJ+lmsJQ= X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417167475 news.xs4all.nl 2829 [2001:888:2000:d::a6]:32879 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:81673 On 11/27/2014 10:31 PM, Seymore4Head wrote: > On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel > 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__. > You've already got self in the __str__ method, or you wouldn't have access to self.hand. But there's no characteristic of 'self' that has any idea of a name like "dealer" or "player". You have to add that if you want it, as I suggested in my first guess. Steven has shown you as well, along with a better explanation. An object does NOT know the name or names that may be bound to it, any more than I know what page of the county register has my birth certificate recorded. If I want to know my own name, I'd better remember it. Along with any nicknames I want to respond to. The way to do it is the same way to know the hand that I hold, make an instance attribute. And the place to do that is in the __init__() method. class Hand: def __init__(self, myname): self.hand = [] # create Hand object self.name = myname def __str__(self): s = self.name + ' contains ' for x in self.hand: s = s + str(x) + " " return s dealer=Hand("Dealer") player=Hand("Player") -- DaveA