getter and setter and list appends

dasacc22 dasacc22 at gmail.com
Fri Apr 24 09:41:26 EDT 2009


On Apr 24, 4:04 am, Piet van Oostrum <p... at cs.uu.nl> wrote:
> >>>>> dasacc22 <dasac... at gmail.com> (d) wrote:
> >d> Ah thank you for clarifying, I did confuse instance and class
> >d> attributes from creating the list in the class def. I actually just
> >d> spiffed up that class to represent a portion of a much larger class
> >d> that needs getter and setter for children. Doing as you said fixed my
> >d> problem, heres the code as reference for w/e
> >d> class Widget(object):
> >d>     _children = None
> >d>     _parent = None
>
> You still have them as class variables here. Now they are only used as
> defaults because you assign to them in the instances so the instance
> variables will be created then. But I think it is still confusing to
> have these class variables here that you never use as such. Maybe this
> is some leftover from Java experience where you do declare instance
> variables at the class level?
>
> >d>     def __init__(self, parent=None):
> >d>         self.children = []
> >d>         self.parent = parent
> >d>     @property
> >d>     def children(self):
> >d>         return self._children
> >d>     @children.setter
> >d>     def children(self, obj):
> >d>         self._children = obj
>
> What is the added value of using a property for the children attribute?
> Why not just use an instance variable directly? Also a Java inheritance?
>
> --
> Piet van Oostrum <p... at cs.uu.nl>
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p... at vanoostrum.org

Hi, yes, you are right, this is from previous experience, and thank
you for bringing this out. It would be better suited to move those
class variables to a comment to stay comfortable perhaps or eliminate
them altogether.

The property method of parent and children actually calls a
_set_as_parent() and _set_as_child() method after setting the private
variable to pack the object for display purposes so that children can
be detached from the parent (becoming its own parent) as a gui event.



More information about the Python-list mailing list