getter and setter and list appends

Piet van Oostrum piet at cs.uu.nl
Fri Apr 24 04:04:40 EDT 2009


>>>>> dasacc22 <dasacc22 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 <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list