getter and setter and list appends

Rhodri James rhodri at wildebst.demon.co.uk
Mon Apr 20 22:17:30 EDT 2009


On Mon, 20 Apr 2009 18:18:39 +0100, dasacc22 <dasacc22 at gmail.com> wrote:

> Ah thank you for clarifying, I did confuse instance and class
> attributes from creating the list in the class def. I actually just
> spiffed up that class to represent a portion of a much larger class
> that needs getter and setter for children. Doing as you said fixed my
> problem, heres the code as reference for w/e
>
> class Widget(object):
>     _children = None
>     _parent = None
>
>     def __init__(self, parent=None):
>         self.children = []
>         self.parent = parent
>
>     @property
>     def children(self):
>         return self._children
>
>     @children.setter
>     def children(self, obj):
>         self._children = obj
>
>     @property
>     def parent(self):
>         return self._parent
>
>     @parent.setter
>     def parent(self, obj):
>         if obj:
>             print obj
>             obj.children.append(self)
>             self._parent = obj

1.  Please don't top-post, it really doesn't help legibility.
2.  This is an artist's impression of a photograph of a greek translation
of your code.  I wouldn't use it as a reference for anything except how
to terminally confuse yourself.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list