Search code examples
pythonenthoughttraits

Traits List handler does not react to += list extension


from traits.api import List, HasTraits

class Foo(HasTraits):
   x = List
   def _x_items_changed(self,new):
      print new.added

f = Foo()
f.x = [1,3]
f.x.append(9) #handler reacts!
f.x += [9,10] # handler does not react! Why?

What am I missing here?

Thanks!


Solution

  • It's a bug. TraitListObject overrides extend() and other mutating methods to propagate changes, but __iadd__() was overlooked. The workaround is to use extend() instead of +=.