「Dev:2.4/Source/Python/Getters and Setters」の版間の差分

提供: wiki
< Dev:2.4‎ | Source‎ | Python
移動先: 案内検索
 
(1版 をインポートしました)
 
(相違点なし)

2018年6月29日 (金) 02:45時点における最新版

Getters and Setters should be implemented so the output of a getter can be used as input to a setter. This lets us write code like this:

a.setFoo( b.getFoo() )

Another common programming idiom that relies on getters and setter co-operating looks like this:

old_value = a.getFoo()  # save old state
a.setFoo( new_val )     # new state
## do something
a.setFoo( old_value )   # restore initial state

If your get/set pair is commonly used like this, consider having the setter return the current state:

old_value = a.setFoo( new_val )
## do something
a.setFoo( old_value )

If you are writing a sexy, modern Bpy type, you could write a tp_getattr handler using a PyMethodDef table. See [[1]] for more information.


-- StephenSwaney - 19 Mar 2005