traits notification for Enum with dynamic values

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

traits notification for Enum with dynamic values

Alexander Held

I have trouble setting up a traits notification for an Enum trait with
dynamic values as in the following example:

from traits.api import HasTraits, Enum, Int, List

class Foo(HasTraits):
     x = Enum(values='xs')
     xs = List(Int)

def x_changed(x):
     print 'notification: x changed to %s' % (x, )

f = Foo()
f.on_trait_change(x_changed, 'x')
print f.x
f.xs = [1, 2]
print f.x
f.x = 2
print f.x

which gives the following output:

notification: x changed to 2

The notification is only fired, when f.x is set explicitly, but not,
when f.x changes implicitly through f.xs. How can I listen for all
changes to f.x? I'm particularly interested in a solution that does not
exploit the fact, that f.x is implemented as an Enum trait with values
f.xs, since in my case, this is an implementation detail, which is not
visible to the part of code that sets up the notification.
Enthought-Dev mailing list
[hidden email]