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


Groups > comp.lang.python > #81658

Re: Can you use self in __str__

Path csiph.com!usenet.pasdenom.info!news.etla.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.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 OK 0.195
X-Spam-Level *
X-Spam-Evidence '*H*': 0.63; '*S*': 0.02; 'method.': 0.07; '__init__': 0.09; 'suggestions.': 0.09; 'python': 0.11; 'def': 0.12; 'name):': 0.16; 'traceback.': 0.16; 'wrote:': 0.18; 'version.': 0.19; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'class.': 0.26; 'player': 0.26; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'prints': 0.31; 'class': 0.32; 'summary': 0.32; 'skip:_ 10': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'should': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'quote': 0.39; 'to:addr:python.org': 0.39; 'subject:Can': 0.60; 'tell': 0.60; 'establish': 0.61; "you're": 0.61; 'show': 0.63; 'name': 0.63; 'real': 0.63; 'reads': 0.68; 'received:74.208': 0.68; 'hand': 0.80; 'dealer': 0.84; 'hand.': 0.84; 'player.': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'thing,': 0.91
Date Thu, 27 Nov 2014 21:49:29 -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>
In-Reply-To <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:uzn4oOyMeMJnRrMYk2fX1dkviPMwkyzOinr+irHcwn3 KnrcBPDaKGCM3/nQkhorprYHvd1WRYMHtBTddo9SAQVw6zC8No XtETKSLBJDUSt8wnKv7KTrf0nEWrxNnJIQ+8Hd5L/83cVLWr+I 5HwP5F/VyzC9ZA+KDb+4hvgMBHSfEO0QiYVuyaLqNQ0HAa4bU8 s3RPJS8RhCuBch3Xl80giV6uvAnxiqdcnopz59rVeXlzePkjfX 3R1m5iHn0Ib2e6oagLntx+9me8905DZrFeNVZjrqt8JyjUH76N CldnH6zXjC9lTJrgF0M1uymVS/cB6ra3clUxwN1rOhq2n2ZO+G EF9POq8HPMRPR2MPztrQ=
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 <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.16388.1417142987.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417142987 news.xs4all.nl 2889 [2001:888:2000:d::a6]:56141
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81658

Show key headers only | View raw


On 11/27/2014 08:26 PM, Seymore4Head wrote:
>      def __str__(self):
>          s = "Hand contains "
>          for x in self.hand:
>              s = s + str(x) + " "
>          return s
>
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()

Good so far. But you neglect to tell us Python version.

> This

What's "this" ?  Please include real code, not just vague suggestions.

> 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

You don't show us the __init__ method.  That's where you should 
establish a "name" for the particular instance of Hand.

class Hand(object):
     def __init__(self, name):
         self.name = name
         self.hand =   something ...

     def __str__(self):
           s = self.name + " contains "
           for x in self.hand:
               s = s + str(x) + " "
           return s


When you quote an error, you should be including the whole thing, as 
well as showing the code that triggers it.  In this message you're 
giving us only disjoint fragments, and one line summary of the traceback.

-- 
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