Accessing container's methods

Vincent Vande Vyvre vincent.vande.vyvre at telenet.be
Tue Dec 8 14:54:25 EST 2015


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 "<stdin>", line 1, in <module>
   File "<stdin>", line 2, in Child
NameError: name 'self' is not defined




More information about the Python-list mailing list