I would like to mix a HasTraits with a standard python object using multiple inheritance. When i do this, the getter/setter methods of the standard object dont function as expected. The example below demonstrates this.
from traits.api import HasTraits
class A(object):
@property
def name(self):
print 'getter'
try:
return self._name
except(AttributeError):
return 'nobody'
@name.setter
def name(self, val):
print 'setter'
self._name = val.upper()
class B(A, HasTraits):
pass
b = B()
b.name #calls getter
b.name = 'name' # doesnt call setter
b.name # doesnt call getter
I assume this is because the HasTraits class intercepts the standard get/set methods. Is there a way around this?
I'm pretty sure there is not a way to make traits play nicely with the native python decorator
@property. At least, not any reasonably clean way.However traits has its own
Propertyconcept with getters and setters. You can define aPropertytrait which does what you want as a simple getter/setter.And if you subclass a
HasTraitswhich contains aProperty, that traits-specific getter/setter effects will be preserved in the subclass. So there is no reason -- that is if you already want the type-checking of traits in your program -- to use the@propertydecorator and not thePropertygetter in the first place.