3.1.2
The name Parameter
The name parameter of
on_trait_change() and on_trait_event() provides significant flexibility in
specifying the name or names of one or more trait attributes that the handler
applies to. It supports syntax for specifying names of trait attributes not
just directly on the current object, but also on sub-objects referenced by the
current object.
The name parameter can take
any of the following values:
·
Omitted, None, or 'anytrait': The handler applies to any trait attribute on
the object.
·
A name or list of names: The handler applies to each trait
attribute on the object with the specified names.
·
An extended name or list of extended names: The handler applies
to each trait attribute that matches the specified extended names.
3.1.2.1
Syntax
Extended names use the following syntax:
xname ::= xname2['.'xname2]*
xname2 ::=
( xname3 | '['xname3[','xname3]*']' ) ['*']
xname3 ::= xname
| ['+'|'-'][name] |
name['?' | ('+'|'-')[name]]
A name is any valid Python
attribute name.
3.1.2.2
Semantics
Table 4 Semantics of extended name notation
|
Pattern
|
Meaning
|
|
item1.item2
|
A trait named item1 contains an object (or objects, if item1 is a list or dictionary), with a trait named item2. Changes to either item1
or item2 cause a notification to be generated.
|
|
item1:item2
|
A trait named item1 contains an object (or objects, if item1 is a list or dictionary), with a trait named item2. Changes to item2
cause a notification, while changes to item1 do
not (i.e., the ‘:’ indicates that changes to the link
object should not be reported.
|
|
[item1, item2, …, itemN]
|
A list that matches any of
the specified items. Note that at the topmost level, the surrounding square
brackets are optional.
|
|
item[]
|
A trait named item is a list. Changes to item
or to its members causes a notification.
|
|
name?
|
If the current object does
not have an attribute called name, the
reference can be ignored. If the ‘?’ character is omitted, the current object
must have a trait called name; otherwise, an
exception is raised.
|
|
prefix+
|
Matches any trait attribute
on the object whose name begins with prefix.
|
|
+metadata_name
|
Matches any trait on the
object that has a metadata attribute called metadata_name.
|
|
-metadata_name
|
Matches any trait on the
current object that does not have a metadata
attribute called metadata_name.
|
|
prefix+metadata_name
|
Matches any trait on the
object whose name begins with prefix and that
has a metadata attribute called metadata_name.
|
|
prefix-metadata_name
|
Matches any trait on the
object whose name begins with prefix and that
does not have a metadata attribute called metadata_name.
|
|
+
|
Matches all traits on the
object.
|
|
pattern*
|
Matches object graphs where pattern occurs one or more times. This option is
useful for setting up listeners on recursive data structures like trees or
linked lists.
|
ExamplesTable 5 Examples of extended name notation
|
Example
|
Meaning
|
|
'foo,
bar, baz'
|
Matches object.foo, object.bar, and object.baz.
|
|
['foo', 'bar', 'baz']
|
Equivalent to 'foo, bar, baz', but may be useful in cases where the
individual items are computed.
|
|
'foo.bar.baz'
|
Matches object.foo.bar.baz
|
|
'foo.[bar,baz]'
|
Matches object.foo.bar and object.foo.baz
|
|
'foo[]'
|
Matches a list trait on object named foo.
|
|
'([left,right]).
name*'
|
Matches the name trait of each tree node object that is
linked from the left or right traits of a parent node, starting with
the current object as the root node. This pattern also matches the name trait of the current object, as the left and right
modifiers are optional.
|
|
'+dirty'
|
Matches any trait on the
current object that has a metadata attribute named dirty set.
|
|
'foo.+dirty'
|
Matches any trait on object.foo that
has a metadata attribute named dirty set.
|
|
'foo.[bar,-dirty]'
|
Matches object.foo.bar or any trait on object.foo that does not have a metadata attribute
named dirty set.
|
For a pattern that references multiple objects, any of the
intermediate (non-final) links can traits of type Instance, List, or Dict. In
the case of List or Dict traits, the subsequent portion of the pattern is applied
to each item in the list or value in the dictionary. For example, if self.children is a list, a handler set for 'children.name' listens for
changes to the name trait for each item in
the self.children list.
The handler routine is also invoked when items are added
or removed from a list or dictionary, because this is treated as an implied
change to the item’s trait being monitored.
|