| 1 |
|
| 2 |
======== |
| 3 |
Appendix |
| 4 |
======== |
| 5 |
|
| 6 |
.. toctree:: |
| 7 |
:hidden: |
| 8 |
|
| 9 |
listening.rst |
| 10 |
|
| 11 |
|
| 12 |
Trait Notification with **on_trait_change** |
| 13 |
-------------------------------------------- |
| 14 |
|
| 15 |
See :ref:`on-trait-change-notification` for details on how to set up change |
| 16 |
notifications using an older API. |
| 17 |
|
| 18 |
|
| 19 |
Property **depends_on** |
| 20 |
----------------------- |
| 21 |
|
| 22 |
The **depends_on** attribute on a **Property** trait allows change |
| 23 |
notifications to be fired for the property when one of its dependents has |
| 24 |
changed. This makes use of **on_trait_change** internally and therefore this |
| 25 |
is an older API. See :ref:`caching-a-property-value` for the current API. |
| 26 |
|
| 27 |
.. index:: depends_on metadata |
| 28 |
|
| 29 |
For example:: |
| 30 |
|
| 31 |
from traits.api import HasStrictTraits, List, Int, Property |
| 32 |
|
| 33 |
class TestScores(HasStrictTraits): |
| 34 |
|
| 35 |
scores = List(Int) |
| 36 |
average = Property(depends_on='scores') |
| 37 |
|
| 38 |
def _get_average(self): |
| 39 |
s = self.scores |
| 40 |
return (float(reduce(lambda n1, n2: n1 + n2, s, 0)) / len(s)) |
| 41 |
|
| 42 |
The **depends_on** metadata attribute accepts extended trait references, using |
| 43 |
the same syntax as the on_trait_change() method's name parameter, described in |
| 44 |
:ref:`the-name-parameter`. As a result, it can take values that specify |
| 45 |
attributes on referenced objects, multiple attributes, or attributes that are |
| 46 |
selected based on their metadata attributes. |