-
-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
execution order of trait observers #874
Comments
based on this: I implemented something like this for my use case: class Test(tr.HasTraits):
c = tr.Bool(default_value=True)
b = tr.Bool(default_value=True)
a = tr.Bool(default_value=True)
@tr.observe("a")
def _a(self, on_change):
print(f"a = {self.a}")
@tr.observe("b")
def _b(self, on_change):
print(f"b = {self.b}")
@tr.observe("c")
def _c(self, on_change):
print(f"c = {self.c}")
@classmethod
def trait_order(cls):
return [k for k, v in cls.__dict__.items() if isinstance(v, tr.TraitType)]
def __init__(self, **kwargs):
super().__init__()
kwargs = self.get_ordered_kwargs(kwargs)
{setattr(self, k, v) for k, v in kwargs.items()}
def get_ordered_kwargs(self, kwargs):
in_order = list(kwargs.keys())
tr_order = self.trait_order()
out_order = tr_order + [i for i in in_order if i not in tr_order]
return {o: kwargs[o] for o in out_order}
t1 = Test(a=False, b=False, c=False)
print("---")
t2 = Test(c=False, b=False, a=False)
# c = False
# b = False
# a = False
# ---
# c = False
# b = False
# a = False I'll leave the isse open for a while in case someone can suggest a "more traitlets native" way to approach this |
Might not be directly related, but as a note, if multiple callbacks created with Would be better to follow the definition order though. class Test(tr.HasTraits):
trait = tr.Bool(default_value=True)
@tr.observe("trait")
def _b(self, on_change):
print("From _b")
@tr.observe("trait")
def _c(self, on_change):
print("From _c")
@tr.observe("trait")
def _a(self, on_change):
print("From _a")
t = Test()
t.trait = False
# From _a
# From _b
# From _c This is due to |
its not clear to me in what order the trait observers get executed and why?
can anyone shed light on this?
many thanks
The text was updated successfully, but these errors were encountered: