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 On Apr 20, 1:05 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote: > dasacc22 wrote: > > Hi, > > > I seem to be having a problem with a list being share across multiple > > instantiations of it and dont quite understand why this is happening. > > > My class looks like this, > > > class Widget(object): > > _parent = None > > _children = [] > > > def __init__(self, parent=None): > > self.parent = parent > > > @property > > def children(self): > > return self._children > > > @property > > def parent(self): > > return self._parent > > > @parent.setter > > def parent(self, obj): > > if obj: > > obj._children.append(self) > > self._parent = obj > > > now if i make instances and attach children like so > > > a = Widget() > > b = Widget(a) > > c = Widget(a) > > d = Widget(c) > > > Basically all the objects end up sharing the _children list from `a` > > instead of forming something like a tree. Any advice would be greatly > > appreciated. > > The problem stems from you confusing instance attributes with class > attributes. You use a latter, which is shared amongst *all* instances of a > given class. > > If you want instance-attributes, use > > def __init__(self, ..): > self.children = [] > > also, your use of properties for the children is pointless in Python. Just > use "children" as attribute name. Then, if at one fine day you want to do > something more than just returning that value, you can still do that witout > changing the interface by using a property - as you do so for parent. > > Diez -- http://mail.python.org/mailman/listinfo/python-list