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


Groups > comp.lang.python > #81659

Re: Can you use self in __str__

Return-Path <i@introo.me>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.085
X-Spam-Evidence '*H*': 0.83; '*S*': 0.00; 'else:': 0.03; 'sys': 0.07; 'string': 0.09; '[1,': 0.09; 'namespace': 0.09; 'obj': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '@property': 0.16; 'module': 0.19; 'first.': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'appreciated': 0.26; 'class.': 0.26; 'player': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'included': 0.31; 'end,': 0.31; 'indentation': 0.31; 'mod': 0.31; 'prints': 0.31; 'anyone': 0.31; 'class': 0.32; 'this.': 0.32; 'received:209.85.212': 0.32; 'level.': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'received:209.85': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'in.': 0.36; 'level': 0.37; 'received:209': 0.37; 'little': 0.38; 'does': 0.39; 'bad': 0.39; 'subject:Can': 0.60; 'break': 0.61; 'john': 0.61; 'name': 0.63; 'field': 0.63; 'skip:n 10': 0.64; 'charset:iso-2022-jp': 0.65; 'reads': 0.68; 'lives': 0.74; 'inform': 0.78; 'hand': 0.80; 'dealer': 0.84; 'dealer,': 0.84; 'hand.': 0.84; 'player.': 0.84; 'resides': 0.84; 'skip:\x1b 30': 0.84; 'subject:self': 0.84; 'subject:you': 0.87
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=20KsaeIzK+vDO7b6iMz0SVldwkFd8rYKQO6T5wLookE=; b=V79A4bfAYMHKDBu7wzvOQxLvHl/WXZFu6oxDQ+DA9T1Y/1AxBvnYOkKme8c6flDe4+ r23/NDXjG+AW/HugLqElJpBn5h2hono109MMo+McfxYUcumHCWZ/2ufx2bDYSYwtzzIC hQ10wbI8q2RHvN2BE58HeqRb5mhzGFctAF9WHWFTwaxwdLnMGBV+lOAn9/7nJ34bqZCc E1xagYTzl067AsXEtrBHc7NnOAwlzjUJUsrm1uUMk8qXGZQBWhJmGrYNtLwkPHST+qfg CouECQwkKqk9MWqvIk5ey43GMfTotvd+JhFi8aHL7cdTV7PNYovGkv/acmD826NzOjfb AbyA==
X-Gm-Message-State ALoCoQk03NbHZocIDr4hc08qsa9EP8CLIoVotz9LKpF7FD/o/9ctN/rThn/20o7xl59CzveDU+Nq
MIME-Version 1.0
X-Received by 10.180.198.211 with SMTP id je19mr3986535wic.27.1417143866762; Thu, 27 Nov 2014 19:04:26 -0800 (PST)
X-Originating-IP [143.89.99.158]
In-Reply-To <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com>
References <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com>
Date Fri, 28 Nov 2014 11:04:26 +0800
Subject Re: Can you use self in __str__
From Shiyao Ma <i@introo.me>
To Seymore4Head <Seymore4Head@hotmail.invalid>
Content-Type text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding 7bit
Cc Python <python-list@python.org>
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.16389.1417143874.18130.python-list@python.org> (permalink)
Lines 82
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417143874 news.xs4all.nl 2913 [2001:888:2000:d::a6]:60698
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:81659

Show key headers only | View raw


2014-11-28 9:26 GMT+08:00 Seymore4Head <Seymore4Head@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.
> 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

I bet you want the object name (aka, dealer, player) be included in
the string 's'.
To that end, you need to access the namespace where 'self' is in.
But I dunno where the namespace 'self' resides in.
Does PyObject has a field to store the namespace of an object?
Appreciated if anyone could
inform me on this.

Now, make a little assumption that the instance lives in the module
level. Then we can do
this:

#!/usr/bin/env python

class Hand(object):
def __init__(self):
self.hand = [1, 2, 3, 4]

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

@property
def _myname(self):
# get the module
mod = self.__module__
import sys
ns = vars(sys.modules[mod])
# NB only works the instance is at the module level
for name, obj in ns.iteritems():
if id(obj) == id(self):
break
else:
#nothing found
return ""
return name

John = Hand()
print(John)

# this prints
# John hand contains 1 2 3 4

bad indentation with my wasavi plugin, see paste:

https://bpaste.net/show/f5b86957295f


What if it's in the local namespace of a function or method? IDK, try
to get that thing first.


Regards






--

吾輩は猫である。ホームーページはhttp://introo.me。

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