据我所知,没有允许检查特征同步状态的官方 API。
当然,您可以简单地sync_trait()
再次调用该方法以确保特征是同步的(或者不同步,如果您使用remove=True
)。结果,您将知道特征的同步状态。
如果您不想更改同步状态,则必须依赖非官方 API 函数,这些函数没有文档记录并且可能会发生变化——因此使用它们需要您自担风险。
from traits.api import HasTraits, Float
class AA(HasTraits):
a =Float()
class BB(HasTraits):
b = Float()
aa = AA()
bb = BB()
aa.sync_trait("a", bb, "b")
# aa.a and bb.b are synchronized
# Now we use non-official API functions
info = aa._get_sync_trait_info()
synced = info.has_key("a") # True if aa.a is synchronized to some other trait
if synced:
sync_info = info["a"] # fails if a is not a synchronized trait
# sync_info is a dictionary which maps (id(bb),"b") to a tuple (wr, "b")
# If you do not know the id() of the HasTraits-object and the name of
# the trait, you have to loop through all elements of sync_info and
# search for the entry you want...
wr, name = sync_info[(id(bb), "b")]
# wr is a weakref to the class of bb, and name is the name
# of the trait which aa.a is synced to
cls = wr() # <__main__.BB at 0x6923a98>
同样,使用风险自负,但它对我有用。