Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.python Subject: Re: Accessing container's methods Date: Tue, 08 Dec 2015 20:02:10 +0100 Organization: PointedEars Software (PES) Lines: 68 Message-ID: <5516674.oipO6xLiNU@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1449601332 8732 eJwFwYEBwCAIA7CXLLa4d4DZ/08w0U7kHKaSsjwmmgCqI4jpPJ7IsKK+WcapdZs75lLl/wEcYxFr (8 Dec 2015 19:02:12 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 8 Dec 2015 19:02:12 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNxMEBwCAIA8CVhCRQxrEK+4/Q3uOEsDjJUFCj4f73TQYfHSQyxulL+a7qQfvFbt6+9cjAI7fCSnixrPMDLZcUHw== Cancel-Lock: sha1:/0mWv4dtMlR3tJu3TMmhxLxKntM= X-NNTP-Posting-Host: eJwFwYEBgDAIA7CXJowWzxEo/59gEo4HzYvAjY2dqWVLGnyaUNJl2RieU1LDZKDlu+Wk4QdPWhIK Xref: csiph.com comp.lang.python:100171 Erik wrote: ^^^^ Please fix, Erik #75656. > On 07/12/15 18:10, Tony van der Hoff wrote: >> 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. > > Generally, an object should not need to know which container it's in NAK. All kinds of objects already "know" which container they are in. > (let alone its "index" or "key" in that container). That is a different issue. > Amongst other things, you can't put the object into multiple containers You could if you made it a list of container references. > which might be organised differently and you are asking for bugs where the > container and the object get out of sync WRT just where the object is in > the container It is the container’s job to make sure that this does not happen. >> Can anyone please advise me on how to achieve this magic? > > As you can't sensibly put the object into more than one container at a > time anyway, You can. Quickhack: class Child: self._parents = [] def add_to_parent (self, parent): self._parents.append(parent) self._parents = list(set(self._parents)) def get_parents (self) return self._parents class Parent: self._children = [] def add_child (self, child): self._children.append(child) child.add_to_parent(self) | >>> p = Parent() | >>> c = Child() | >>> p.add_child(c) | >>> p2 = Parent() | >>> p2.add_child(c) | >>> c.get_parents() | [p, p2] “As a child, I want to know who my parents are.” Certainly you will not deny the validity of that user story ;-) -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.