list property fires get on append

ian at neustyle.com ian at neustyle.com
Sun Jan 6 02:21:54 EST 2008


I've created a class that has a property which points at a private
list.  When I try to use the append() function on this list property,
the fget method is fired rather than the fset method.  If I directly
set my property to a literal list, the set method fires.

Here's a stripped down version of my code:

class Hierarchy(object):
    _children = []

    def __init__(self):
        return

    def get_children(self):
        print("GETTING")
        return self._children

    def set_children(self, value):
        print("SETTING")
        self._children = value

    children = property(get_children, set_children)

-----USAGE------

import Hierarchy
hierarchy = Hierarchy.Hierarchy()
# this fires a get for some reason
hierarchy.children.append( Hierarchy.Hierarchy())
# this fires a set as expected
hierarchy.children = [Hierarchy.Hierarchy()]

------RESULT------

it prints:

GETTING
SETTING



More information about the Python-list mailing list