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

前端 未结 3 2015
栀梦
栀梦 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:24

    I recommend you look into using numpy. In particular, the transpose method gives a "view" of the underlying matrix with the axes transposed, meaning that if you change one then the other will change.

    If that doesn't suit your needs, then the only way I can see to make the API you want work is to define the "rows" and "columns" methods to return custom "view objects" that don't actually store the data, but that point back to some private (shared) data in the grid object itself. These could be list-like, but would not support some operations (e.g., append or clear).

提交回复
热议问题