python: how to have a property and with a setter function that detects all changes that happen to the value

前端 未结 3 2014
栀梦
栀梦 2020-12-18 07:50

I have a two properties which holds lists. Whenever any item in this list changes, I would like the other list to update itself. This includes the statement obj.myProp

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 08:28

    if you simply return your rows or columns list, then you'll never have any control about what happens when an item is changed.

    one possibility would be not to provide properties to get/set the lists directly, but provide setters/getters for an item at position x/y.

    a nice version would be to have __setitem__/__getitem__ and have them accept a tuple, that way you could acces the elements using foo[x,y] and foo[x,y] = bar.

    an other way would be to return a wrapper around the list that detects whe an element is changed, but then you'll also have to do that for every nested list.

提交回复
热议问题