Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100103

Re: Accessing container's methods

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Accessing container's methods
Date Mon, 7 Dec 2015 12:33:41 -0700
Lines 32
Message-ID <mailman.28.1449516863.12405.python-list@python.org> (permalink)
References <dcm0c2Fi2ktU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de CdyX8monN8FD2w7azXwmhg7BoJ9J/B1vze4GJxJ4Ewig==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'static': 0.03; 'classes,': 0.05; 'classes.': 0.07; 'garbage': 0.09; 'noted,': 0.09; 'python': 0.10; '__del__': 0.16; 'class;': 0.16; 'contrived': 0.16; 'former,': 0.16; 'guessing': 0.16; 'java.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sorts': 0.16; 'sub-class': 0.16; 'subject:Accessing': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'nested': 0.18; 'creates': 0.18; '2015': 0.20; 'java': 0.22; 'cycles': 0.22; 'pass': 0.22; 'trying': 0.22; 'am,': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'van': 0.26; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'looks': 0.29; 'implicitly': 0.29; 'objects': 0.29; 'raise': 0.29; "i'm": 0.30; 'anywhere': 0.30; 'creating': 0.30; 'class.': 0.30; 'anyone': 0.32; 'embedded': 0.32; 'class': 0.33; 'problem': 0.33; 'received:google.com': 0.35; 'instance': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'method': 0.37; 'setting': 0.37; 'received:209.85.213': 0.37; 'associated': 0.38; 'received:209': 0.38; 'self': 0.38; 'hi,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'save': 0.60; 'different': 0.63; 'here': 0.66; 'to:name:python': 0.84; 'actor,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=1WKifhkE8vK3uLa7ZMiDpHW5TKUsUCeteU+hp6CBuTk=; b=NTtoMkPL3/X5PQOJ0i/w8BqXL2QdFCBce1VkkYw+k2yswLyRmcLLj95XHY544lZaqs RwtMyfOumm0ipB6c6yRDFpKjUW2o6zjiuERc/pifUbt5ELYrIh+3NqRuJ7rivK++xUob 4hjsTmVFzEfm0J4ry/El0L6vObOTKodHJmTCruVc/w3692lmga9Qgn/FzjgDwoT9Gf+N nRpSsz5wXmwSHFQtsz7J2e6wkMbEguu5dleyo2jcmky+59hGewxvAMX90JVBKUcclfto dGgDpGiMOz3293V4VhmmcQo5ZMvc6pVXwtsGOsHpWGPsH8oWRetZ/nsBOA7vBJQ7XtTo XWtA==
X-Received by 10.50.109.136 with SMTP id hs8mr31682igb.93.1449516860953; Mon, 07 Dec 2015 11:34:20 -0800 (PST)
In-Reply-To <dcm0c2Fi2ktU1@mid.individual.net>
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:100103

Show key headers only | View raw


On Mon, Dec 7, 2015 at 11:10 AM, Tony van der Hoff <tony@vanderhoff.org> 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?

I'm guessing that you're coming from Java. Java has two different
types of nested classes: non-static, where an instance of the inner
class is implicitly associated with an instance of the outer class;
and static, where an instance of the inner class is unrelated to any
instance of the outer class.

It looks like you're trying to do the former, but Python only has the
static type of nested classes. So how would you go about creating a
nested class of the non-static type? Make the association explicit.
When Monty creates an Actor, pass it self as one of the arguments.
Actor can then save that instance of Monty in an attribute and call
the method thusly.

As others have noted, this does create a reference cycle. The Python
garbage collector is fine with cleaning up reference cycles as long as
there are no objects __del__ methods anywhere in the cycle, so it's up
to you whether you consider that to be a problem or not.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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