Dev:2.4/Source/Python/Getters and Setters

提供: wiki
< Dev:2.4‎ | Source‎ | Python
2018年6月29日 (金) 02:45時点におけるYamyam (トーク | 投稿記録)による版 (1版 をインポートしました)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

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