user3391229
user3391229

Reputation: 818

traits api creating new trait and trait changed behavior

I often find myself in this situation:

class A:...

B=class
    a=Instance(A,())
    @on_trait_change('a')##I would really like to be able to do this
    def do_something(...)

I think that this currently triggers if you were to reset the entirety of the class. e.g. b=B(). b.a=A() should trigger it. But I would like to control when my custom class signals that it has been 'changed'. Per haps one might like A to signal 'changed' if merely a member of A is changed e.g. b.a.x+=1

Upvotes: 0

Views: 85

Answers (1)

Chris Farrow
Chris Farrow

Reputation: 156

If both A and B derive from HasTraits, then changing your decorator to @on_trait_change('a.+') will do what you want. If you change the signature of your do_something to two or more arguments, you'll even be able to detect which attributes of a changed. (See http://traits.readthedocs.org/en/latest/traits_user_manual/notification.html#notification-handler-signatures.)

Upvotes: 2

Related Questions