Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100101
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: Accessing container's methods |
| Date | Mon, 07 Dec 2015 19:59:54 +0100 |
| Organization | None |
| Lines | 124 |
| Message-ID | <mailman.27.1449514812.12405.python-list@python.org> (permalink) |
| References | <dcm0c2Fi2ktU1@mid.individual.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de oPfuJ1oCiuCaT28y4omdSQib+Tw5RvTqnrfkanNAsjaw== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'classes,': 0.05; 'none:': 0.05; '(self,': 0.07; '__name__': 0.07; 'variant': 0.07; '__init__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'def': 0.13; "'__main__':": 0.16; '1):': 0.16; 'add(self,': 0.16; 'class;': 0.16; 'contrived': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'remove(self,': 0.16; 'sorts': 0.16; 'sub-class': 0.16; 'subject:Accessing': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'library': 0.20; 'class,': 0.22; 'latter': 0.22; 'simpler': 0.22; 'txt': 0.22; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'van': 0.26; 'define': 0.27; 'yield': 0.27; "skip:' 10": 0.28; 'container': 0.29; 'lies': 0.29; 'raise': 0.29; "i'm": 0.30; 'print': 0.30; 'code': 0.30; 'putting': 0.30; 'probably': 0.31; 'anyone': 0.32; 'skip:_ 10': 0.32; 'embedded': 0.32; 'class': 0.33; 'instance': 0.35; '(and': 0.36; 'skip:{ 10': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'setting': 0.37; 'received:org': 0.37; 'names': 0.38; 'self': 0.38; 'skip:p 20': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'collection': 0.60; 'your': 0.60; 'back': 0.62; 'skip:n 10': 0.62; 'course': 0.62; 'here': 0.66; 'container):': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd9067.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| 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> |
| Xref | csiph.com comp.lang.python:100101 |
Show key headers only | View raw
Tony van der Hoff wrote:
> Hi,
>
> I have a class A, containing embedded embedded classes, which need to
> access methods from A.
> .
> A highly contrived example, where I'm setting up an outer class in a
> Has-a relationship, containing a number of Actors. The inner class needs
> to access a method of the outer class; here the method get_name.
>
> I don't really want to make Actor a sub-class (is-a; it isn't) of Monty;
> that would raise all sorts of other problems.
>
> Can anyone please advise me on how to achieve this magic?
>
> # define the outer class
> class Monty:
> def __init__( self, names ):
> self.actors = []
>
> i = 0
> for n in names:
> self.actors.append( Actor( n, i ) )
> i += 1 # here is a case for python supporting post-increment!
>
> def count_actors( self ):
> return len( self.actors )
>
> def list_actors( self ):
> h=[]
> for n in self.actors:
> h.append( n.get_name() )
> return h
>
> # define the inner class
> class Actor:
> def __init__ ( self, name, id ):
> self.name = name
> self.id = id
>
> def get_name( self ):
>
> # and here lies the problem;
> # AttributeError: Actor instance has no attribute 'count_actors'
> # how do I access the method in the enclosing class
> txt = "I'm Actor {} Number {} of {}".\
> format( self.name, self.id, self.count_actors() )
>
> # this works, of course
> #txt = "I'm Actor \"{}\"; Number {}. ".\
> format( self.name, self.id )
>
> return txt
>
> if __name__ == '__main__':
> o = Monty( ["Cleese", "Idle", "Palin" ] )
> print "number: ",o.count_actors()
> a = o.list_actors()
> for l in a:
> print l
I think I've seen the solution a long time a go in a Borland library -- a
Collection and a CollectionItem class, the latter with a reference to the
collection it belongs to. However, you are introducing a reference cycle,
and a simpler solution like putting a print_actor() method into the Monty
class is probably the way to go.
Anyway, here's a variant of your code using the back reference (and a few
cosmetical changes):
__metaclass__ = type # python 2 compatibility
class Monty:
def __init__(self, names):
self.actors = []
for id, name in enumerate(names, 1):
Actor(name, id, container=self)
def __len__(self):
return len(self.actors)
def __iter__(self):
for actor in self.actors:
yield actor
def remove(self, actor):
raise NotImplementedError
def add(self, actor):
self.actors.append(actor)
class Actor:
def __init__ (self, name, id, container=None):
self.name = name
self.id = id
self._container = None
self.container = container
def get_container(self):
return self._container
def set_container(self, container):
if self._container is not None:
self._container.remove(self)
if container is not None:
container.add(self)
self._container = container
container = property(get_container, set_container)
def __repr__(self):
return "I'm Actor {} Number {} of {}".format(
self.name, self.id, len(self.container))
if __name__ == '__main__':
o = Monty( ["Cleese", "Idle", "Palin" ])
print("number: {}".format(len(o)))
for l in o:
print(l)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Accessing container's methods Tony van der Hoff <tony@vanderhoff.org> - 2015-12-07 18:10 +0000
Re: Accessing container's methods Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2015-12-07 18:21 +0000
Re: Accessing container's methods Michael Torrie <torriem@gmail.com> - 2015-12-07 11:36 -0700
Re: Accessing container's methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-07 20:03 +0100
Re: Accessing container's methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-07 19:46 +0100
Re: Accessing container's methods Peter Otten <__peter__@web.de> - 2015-12-07 19:59 +0100
Re: Accessing container's methods Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-07 12:33 -0700
Re: Accessing container's methods Terry Reedy <tjreedy@udel.edu> - 2015-12-07 16:38 -0500
Re: Accessing container's methods Chris Angelico <rosuav@gmail.com> - 2015-12-08 09:02 +1100
Re: Accessing container's methods Erik <python@lucidity.plus.com> - 2015-12-07 23:47 +0000
Re: Accessing container's methods Tony van der Hoff <tony@vanderhoff.org> - 2015-12-08 12:35 +0000
Re: Accessing container's methods [solved] Tony van der Hoff <tony@vanderhoff.org> - 2015-12-08 13:46 +0000
Re: Accessing container's methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-08 20:02 +0100
Re: Accessing container's methods Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2015-12-08 20:54 +0100
Re: Accessing container's methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-08 23:51 +0100
Re: Accessing container's methods Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-08 20:30 +0000
Re: Accessing container's methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-08 23:52 +0100
Re: Accessing container's methods Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-08 22:59 +0000
Re: Accessing container's methods Erik <python@lucidity.plus.com> - 2015-12-08 22:37 +0000
Re: Accessing container's methods Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-08 16:41 -0700
Re: Accessing container's methods Chris Angelico <rosuav@gmail.com> - 2015-12-09 12:04 +1100
csiph-web