You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In #826 a discussion started on how the watchers are specially handled when a Parameter is copied from class to an instance. I've come up with some examples that exhibit some bugs and interesting behavior (e.g. side effects of calling p.param.x).
Class-level watcher on default
store= []
defcb(event): store.append(event)
classP(param.Parameterized):
x=param.Parameter()
P.param.watch(cb, 'x')
P.x=10assertlen(store) ==1print(P.param.x.watchers)
assert'value'inP.param.x.watchersp=P()
print(P.param.x.watchers)
assert'value'inP.param.x.watchersp.param.x# side-effectprint(P.param.x.watchers)
assertP.param.x.watchers== {} # :( Watchers no longer saved on the class Parameterprint(p.param.x.watchers)
assert'value'inp.param.x.watchers# :( Now saved on the instance ParameterP.x=20p.x=30assertlen(store) ==1# Changes at the class or instance level no longer re-trigger the callback.
Class-level watcher on an Parameter attribute (constant)
store= []
defcb(event): store.append(event)
classP(param.Parameterized):
x=param.Parameter()
P.param.watch(cb, 'x', 'constant')
assert'constant'inP.param.x.watchersP.param.x.constant=Trueassertlen(store) ==1p=P()
assert'constant'inP.param.x.watchersprint(P.param.x.watchers)
p.param.x# side-effectassertP.param.x.watchers== {} # :( Watchers no longer saved on the class Parameterprint(P.param.x.watchers)
print(p.param.x.watchers)
assert'constant'inp.param.x.watchers# :( Now saved on the instance Parameterp.param.x.constant=Falseassertlen(store) ==2# Changing constant on the instance triggers the callbackP.param.x.constantisTrue# just checking the valueP.param.x.constant=Falseassertlen(store) ==2# Changes at the class level no longer re-trigger the callback.
Instance-level watcher on value
classP(param.Parameterized):
x=param.Parameter()
l=param.Parameter([])
@param.depends('x', watch=True)defcb(self):
self.l.append(self.x)
print(P.param.x.watchers)
assertP.param.x.watchers== {}
print(P.param._depends)
assertP.param._depends['watch'][0][3][0].what=='value'p=P()
print(P.param.x.watchers)
assertP.param.x.watchers== {}
print(p.param.watchers)
assert'x'inp.param.watchersprint(P.param._depends)
assertP.param._depends['watch'][0][3][0].what=='value'p.x=30assertlen(p.l) ==1# Nothing wrong in this example, just checking how things work
Instance-level watcher on an attribute, two instances
classP(param.Parameterized):
x=param.Parameter()
l=param.List([])
@param.depends('x:constant', watch=True)defcb(self):
print('cb', self.param.x.constant)
self.l.append(self.param.x.constant)
print(P.param.x.watchers)
assertP.param.x.watchers== {}
print(P.param._depends)
assertP.param._depends['watch'][0][3][0].what=='constant'p=P()
print(P.param.x.watchers)
assert'constant'inP.param.x.watchersp2=P()
p2.param.x.constant=Trueassertp2.l== [True] # okassertp.l== [False] # :( Changing the attribute on another instance triggered the callback attached to instance `p` too
Instance-level watcher on an attribute
classP(param.Parameterized):
x=param.Parameter()
l=param.List([])
@param.depends('x:constant', watch=True)defcb(self):
self.l.append(self.param.x.constant)
print(P.param.x.watchers)
assertP.param.x.watchers== {}
print(P.param._depends)
assertP.param._depends['watch'][0][3][0].what=='constant'p=P()
print(P.param.x.watchers)
assert'constant'inP.param.x.watchers# The watchers are temporarily set on the class Parameter?P.param.x.constant=Falseprint(p.l, P.l)
assertp.l==P.l== []
p.param.x# side-effectprint(P.param.x.watchers)
assertP.param.x.watchers== {}
print(p.param.x.watchers)
assert'constant'inp.param.x.watchersp.param.x.constantisFalse# just checking the valuep.param.x.constant=Trueprint(p.l)
assertp.l== [True]
The text was updated successfully, but these errors were encountered:
In #826 a discussion started on how the watchers are specially handled when a Parameter is copied from class to an instance. I've come up with some examples that exhibit some bugs and interesting behavior (e.g. side effects of calling
p.param.x
).default
constant
)value
The text was updated successfully, but these errors were encountered: