Accessing container's methods

Thomas 'PointedEars' Lahn PointedEars at web.de
Tue Dec 8 17:51:10 EST 2015


Vincent Vande Vyvre wrote:

> Le 08/12/2015 20:02, Thomas 'PointedEars' Lahn a écrit :
>> Erik wrote:
>>> 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.

I _think_ you should read postings more carefully before replying to them.
 
>  >>> 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

Can be fixed easily by omitting “self.” there; likewise in Parent.  (In 
other programming languages I know, you need to refer to the class/instance 
explicitly to which you add attributes/properties.)

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list