Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: UNSURE 0.215 X-Spam-Level: ** X-Spam-Evidence: '*H*': 0.60; '*S*': 0.03; 'assuming': 0.09; 'cc:addr :python-list': 0.11; "can't.": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'player': 0.26; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'prints': 0.31; 'fri,': 0.33; "can't": 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'object,': 0.36; 'nov': 0.38; 'pm,': 0.38; 'subject:Can': 0.60; 'simply': 0.61; "you're": 0.61; 'name': 0.63; 'happen': 0.63; 'reads': 0.68; 'hand': 0.80; 'dealer': 0.84; 'hand.': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=m4yC2YRJQfXLCCrQflEU3mDFriGPeBuSnMkHVGGbYHA=; b=sszx8l2Ghvx5EgKw3yxMV9Y6DsFgQA63f/vjp1eS/plYqk9grzF4krK79ghKPMYqec b3eKa2dE24/Qo8WhDEqS/5PlaeRYXLWlBCdKOQdN0HsEXSLiVOk5nMgsnspVaqvt0TMb OXSyneDzVI0v1Pa+V24FdVEB3NPxVIE7wq1aWtLEEX/CXgPxLIdvdzzYC4K+9tb1Kqq3 5GYXgZUrPdPjUJdoGECUtT1kypX+umVmqDI5kVDyHn0SbEkkORPphuT6OgBMRH2YysBn cvW+ZQcQPTEK/+raHFl8UAbizcqQoRZFeqhAg3ZtRsA1AfqMYl1jRZp4DTkwXqRlvv/9 XF+Q== MIME-Version: 1.0 X-Received: by 10.42.38.133 with SMTP id c5mr289796ice.26.1417139032117; Thu, 27 Nov 2014 17:43:52 -0800 (PST) In-Reply-To: <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> References: <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> Date: Fri, 28 Nov 2014 12:43:52 +1100 Subject: Re: Can you use self in __str__ From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-Mailman-Approved-At: Fri, 28 Nov 2014 12:06:55 +0100 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417172816 news.xs4all.nl 2891 [2001:888:2000:d::a6]:51906 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81675 On Fri, Nov 28, 2014 at 12:26 PM, Seymore4Head wrote: > 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 No, you can't. You're assuming that the name bound to an object is somehow part of that object, but it isn't. What would happen if you did this: print(Hand()) There's no name, so you can't get that information. The only way to do it would be to pass that to the Hand object, but you may as well simply print it out separately. ChrisA