0

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

user3391229
  • 798
  • 9
  • 17

1 Answers1

2

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.)

Chris Farrow
  • 156
  • 3
  • Yeah I guess I should settle for that... I doesn't literally do what I want, though – user3391229 Oct 26 '15 at 15:22
  • It sounds like what you want is to manually manage the events yourself, which traits certainly allows you to do. – aestrivex Nov 06 '15 at 18:06
  • @aestrivex Hi, I know this is coming literally 6 years later, but could you elaborate on how to do that? I've been trying to figure out how to manually trigger events from a custom class using the traits API for some time now and haven't managed to find a way. – Blayzeing Oct 19 '21 at 21:57