我正试图完全了解traitlets机制是如何工作的
下面是一个如何观察值的示例,slightly modified from the docs:
from traitlets import HasTraits,Int
class Foo(HasTraits):
bar = Int()
foo = Foo()
def func(change):
print('----------')
print(change['old'])
print(change['new']) # as of traitlets 4.3, one should be able to
print('----------') # write print(change.new) instead
foo.observe(func, names=['bar'])
foo.bar = 1 # prints '0\n 1'
foo.bar = 42 # prints '1\n 42'
print(type(foo)) # prints <class '__main__.Foo'>
print(type(foo.bar)) # prints <class 'int'>
这在幕后是怎么发生的? =运算符是否过载? 为什么需要上课
这里,我们似乎将foo.bar绑定到函数observe,因此我们绑定了特定的foo实例, 我们能不能把全班都绑起来? 这些似乎不止一个问题,但它们都归结为“请解释观察工作的机制”
目前没有回答
相关问题 更多 >
编程相关推荐