【问题标题】:Property-Trait with "depends_on"带有“depends_on”的属性特征
【发布时间】:2015-02-11 05:33:36
【问题描述】:

给定基于特征的类 Material-class、Base-class 和 Child-class(派生自 Base-class),如果 Child-class 的 Property-trait b 仅依赖于 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())

【问题讨论】:

    标签: python enthought traits


    【解决方案1】:

    您可以通过不使用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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多