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


Groups > comp.lang.python > #81676

Re: Can you use self in __str__

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status UNSURE 0.424
X-Spam-Level ****
X-Spam-Evidence '*H*': 0.21; '*S*': 0.05; 'assuming': 0.09; "can't.": 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; "shouldn't": 0.24; 'player': 0.26; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'prints': 0.31; 'terms.': 0.31; 'fri,': 0.33; 'guess': 0.33; "can't": 0.35; 'no,': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'object,': 0.36; 'useful': 0.36; 'should': 0.36; 'nov': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'subject:Can': 0.60; 'tell': 0.60; 'simply': 0.61; "you're": 0.61; "you've": 0.63; 'name': 0.63; 'such': 0.63; 'happen': 0.63; 'reads': 0.68; 'received:74.208': 0.68; 'strategies': 0.77; 'hand': 0.80; 'dealer': 0.84; 'hand.': 0.84; "it'd": 0.84; 'subject:self': 0.84; 'winner,': 0.84; 'subject:you': 0.87; 'concluded': 0.91
Date Fri, 28 Nov 2014 06:43:27 -0500
From Dave Angel <davea@davea.name>
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> <CAPTjJmoibML3N-VXQVve6R=9_T=RUhLC9K5YBOHsFvaFomMROA@mail.gmail.com>
In-Reply-To <CAPTjJmoibML3N-VXQVve6R=9_T=RUhLC9K5YBOHsFvaFomMROA@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:9V9afoAUiQqmrRcTAccnQrVrjjQpEBF9iAxZl9eqLn7 8aQqUTPhQroiydBm2h6t75yz6AXF5uXbgYuDdcG8tiHH1vyYO9 XGl8Eriz4X8131Df6YpGeXgA0m/Hq7Su3JlHIxj8dLPrGbQmAe Dd9nF03RWqwBTFE6iz1kHXOsSre4cR/crNwu+8tbyrlvoFsjc2 zvzfhLibtnAS3vQIMUmaTIWr2aR8/rHe4suvS6buSUrCocLFU1 qyB55EZvnWUa04vZ+ztl0LSLFzA9rZVAqUWACsCR+tc6M1BNK3 5rDd5zfpD77/W/Gi+k8+CsckWMtRqrBDU7lDp2Uq/ocCGaXW+K kzTjNbwFhXg8MswrRgQs=
X-UI-Out-Filterresults notjunk:1;
X-Mailman-Approved-At Fri, 28 Nov 2014 13:09:37 +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 <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.16401.1417176578.18130.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417176578 news.xs4all.nl 2977 [2001:888:2000:d::a6]:60711
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81676

Show key headers only | View raw


On 11/27/2014 08:43 PM, Chris Angelico wrote:
> On Fri, Nov 28, 2014 at 12:26 PM, Seymore4Head
> <Seymore4Head@hotmail.invalid> 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.

It is very useful for a Hand instance to know its "name".

Lots of game strategies treat a collection of such objects identically, 
and then after the fact want to tell them apart.  Printing is one 
example.  But if you've just concluded that object 174fa44 is the 
winner, it'd be nice to be able to tell the user in his own terms.

As you say you shouldn't attempt to guess it, but should pass it into 
the initializer.
-- 
DaveA

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


Thread

Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 20:26 -0500
  Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-27 21:49 -0500
    Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 22:31 -0500
      Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-28 04:37 -0500
  Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-28 11:04 +0800
    Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 22:35 -0500
      Re: Can you use self in __str__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-28 18:44 +1100
  Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-28 16:00 +1100
  Re: Can you use self in __str__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-28 19:25 +1100
  Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-28 12:43 +1100
  Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-28 06:43 -0500
  Re: Can you use self in __str__ Rustom Mody <rustompmody@gmail.com> - 2014-11-28 06:21 -0800
    Re: Can you use self in __str__ Rustom Mody <rustompmody@gmail.com> - 2014-11-28 08:37 -0800
  Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-29 11:16 +0800
  Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-29 14:36 +1100
  Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-29 11:55 +0800

csiph-web