给定基于特征的类Material-class,Base-class和Child-class(从Base-class派生),如果b
Child-class的Property-trait仅依赖于a
或具有x
属性,即运行以下代码,即
b = Property(Float, depends_on=['a'])
或者
b = Property(Float, depends_on=['x'])
但如果同时取决于这两个因素,则不会:
b = Property(Float, depends_on=['a','x'])
为什么?
from traits.api import HasTraits, DelegatesTo, Float, Instance, Range, Property
class Material(HasTraits):
a = Float(10)
class Base(HasTraits):
x = Float(-1)
class Child(Base):
m = Instance(Material)
a = DelegatesTo('m')
# b = Property(Float, depends_on=['a']) # <-- runs
# b = Property(Float, depends_on=['x']) # <-- runs
b = Property(Float, depends_on=['a','x']) # <-- fails
def _get_b(self):
return self.a * self.x
c = Child(m=Material())
您可以通过不使用a来解决此问题,而DelegatesTo
只听以下更改m.a
:
class Child(Base):
m = Instance(Material)
b = Property(Float, depends_on=['m.a','x']) # <-- runs
def _get_b(self):
return self.a * self.x
委托的问题似乎与设置侦听器有关。我收到的错误消息是
DelegationError: The 'a' attribute of a 'Child' object has a delegate which does not have traits.
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句