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


Groups > comp.lang.python > #100171

Re: Accessing container's methods

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.python
Subject Re: Accessing container's methods
Date 2015-12-08 20:02 +0100
Organization PointedEars Software (PES)
Message-ID <5516674.oipO6xLiNU@PointedEars.de> (permalink)
References <dcm0c2Fi2ktU1@mid.individual.net> <mailman.41.1449532084.12405.python-list@python.org>

Show all headers | View raw


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.

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