Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81661
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Can you use self in __str__ |
| Date | 2014-11-27 22:35 -0500 |
| Message-ID | <b6rf7admi10dnub1pq1bebv3f967kn0okm@4ax.com> (permalink) |
| References | <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> <mailman.16389.1417143874.18130.python-list@python.org> |
| Organization | Unlimited download news at news.astraweb.com |
On Fri, 28 Nov 2014 11:04:26 +0800, Shiyao Ma <i@introo.me> wrote: >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'. That is exactly what I want, but your explanation is too complicated for my feeble mind to get just yet. >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
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