Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Vincent Vande Vyvre Newsgroups: comp.lang.python Subject: Re: Accessing container's methods Date: Tue, 8 Dec 2015 20:54:25 +0100 Lines: 41 Message-ID: References: <5516674.oipO6xLiNU@PointedEars.de> Reply-To: vincent.vandevyvre@oqapy.eu Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de Q5owmy/f/TIUp8cZRv3GVAGZOD4CzQqsSIHg7zUOZaOQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(self,': 0.07; 'nameerror:': 0.09; 'def': 0.13; '"key"': 0.16; 'class;': 0.16; 'containers': 0.16; 'contrived': 0.16; 'erik': 0.16; 'fix,': 0.16; 'from:name:vincent vande vyvre': 0.16; 'parent):': 0.16; 'parent:': 0.16; 'received:195.130': 0.16; 'received:195.130.137': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'received :telenet-ops.be': 0.16; 'subject:Accessing': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'issue.': 0.20; '"",': 0.22; 'defined': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'van': 0.26; "skip:' 10": 0.28; 'container': 0.29; 'objects': 0.29; "i'm": 0.30; 'code': 0.30; 'received:be': 0.30; 'post': 0.31; "can't": 0.32; 'class': 0.33; 'traceback': 0.33; 'file': 0.34; 'should': 0.36; 'alone': 0.36; 'child': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'setting': 0.37; 'test': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'thomas': 0.63; 'different': 0.63; 'here': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; '.....': 0.76; '"index"': 0.84; '(let': 0.84; 'container).': 0.84; 'amongst': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <5516674.oipO6xLiNU@PointedEars.de> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100172 Le 08/12/2015 20:02, Thomas 'PointedEars' Lahn a écrit : > 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 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 = [] > ..... I thing you should test your code before post it. >>> class Child: ... self.parents = [] ... Traceback (most recent call last): File "", line 1, in File "", line 2, in Child NameError: name 'self' is not defined