Can you use self in __str__
steve+comp.lang.python at pearwood.info
Fri Nov 28 08:44:24 CET 2014
> On Fri, 28 Nov 2014 11:04:26 +0800, Shiyao Ma <i at introo.me> wrote:
>>2014-11-28 9:26 GMT+08:00 Seymore4Head <Seymore4Head at hotmail.invalid>:
>>> 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.
>>> 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
>>I bet you want the object name (aka, dealer, player) be included in
>>the string 's'.
> That is exactly what I want, but your explanation is too complicated
> for my feeble mind to get just yet.
Shiyao Ma's explanation is too complicated for *my* feeble mind to get, and
I've been using Python for over 15 years. Either I have no idea what Ma is
trying to say, or Ma has no idea what (s)he's trying to say.
>>To that end, you need to access the namespace where 'self' is in.
`self` is a local variable of the method. Accessing the local namespace is
More information about the Python-list