Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Accessing container's methods Date: Mon, 7 Dec 2015 16:38:33 -0500 Lines: 56 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de t7fgtV5YNJYYZ8aLchQR9wrRblck/z832VBFEIrT3O3g== Return-Path: 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; '__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; 'jan': 0.11; 'def': 0.13; 'class;': 0.16; 'contrived': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'sorts': 0.16; 'sub-class': 0.16; 'subject:Accessing': 0.16; 'undo': 0.16; 'wrote:': 0.16; 'circular': 0.18; 'explicit': 0.22; '(or': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'van': 0.26; 'define': 0.27; 'container': 0.29; 'references.': 0.29; 'raise': 0.29; "i'm": 0.30; 'creating': 0.30; 'anyone': 0.32; 'embedded': 0.32; 'class': 0.33; 'but': 0.36; 'faster': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'setting': 0.37; 'received:org': 0.37; 'names': 0.38; 'self': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'here': 0.66; 'tkinter,': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-71-185-227-36.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: 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:100109 On 12/7/2015 1:10 PM, 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! This is actually a case for using enumerate: for i, name in enumerate(names): > > 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 return list(self.actors) # or perhaps even faster return self.actors[:] > # define the inner class > class Actor: > def __init__ ( self, name, id ): > self.name = name > self.id = id Tkinter, and I presume tk, works by creating circular references. widgets get a reference to a master widget, and container get a reference to widgets that are placed (or packed or gridded) therein. Widgets have an explicit .destroy method (inherited from tcl/tk) to undo the circularity. It works, but it is not completely without problems. -- Terry Jan Reedy