Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81673
| Date | 2014-11-28 04:37 -0500 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: Can you use self in __str__ |
| References | <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> <mailman.16388.1417142987.18130.python-list@python.org> <4gpf7ahkq3isr8g7l6cs8dt0anu9oiva57@4ax.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16398.1417167475.18130.python-list@python.org> (permalink) |
On 11/27/2014 10:31 PM, Seymore4Head wrote:
> 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__.
>
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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