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.

Class attributes are shared by all instances.

My class looks like this,

class Widget(object):
    _parent = None
    _children = []

Move this line

    def __init__(self, parent=None):
        self.parent = parent

to here.

    @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.

Thanks,
Daniel
--
http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to