How can I understand the success of the first append but the failure of the second, here? (Mma 13.3.) Is there a ready work around?

DynamicModule[{g = 0.05},
 {Manipulator[Dynamic[g], ContinuousAction -> False],
  Append[List[], Dynamic[g]],
  Append[Association[], "test" -> Dynamic[g]]}

2 Answers 2


In short: Wrap Dynamic around the "construction" of the Association:

 {g = 0.05},
  Manipulator[Dynamic[g], ContinuousAction -> False],
  Append[List[], Dynamic[g]],
  Dynamic@Append[Association[], "test" -> Dynamic[g]]

Manipulate effectively does something along these lines (in fact it wraps Dynamic around everything), which is why it works while your DynamicModule doesn't.

The reason it fails without the Dynamic is a bit involved, but essentially it boils down to the fact that Associations, once constructed, keep their contents unevaluated/protected in some sense (see e.g. this question & related). This means that e.g. Module localization doesn't work within constructed associations (something similar to the following is happening inside DynamicModule):

<|a -> b|> /. x_ :> Module @@ Hold[{b = 2}, x]
(* <|a -> b|> *)

{a -> b} /. x_ :> Module @@ Hold[{b = 2}, x]
(* {a -> 2} *)

Note how the b in the first example has not been replaced by 2 like one might expect. Wrapping Dynamic around the association constructions delays said construction until after DynamicModule has done its job and has localized all the variables.

  • $\begingroup$ Thanks, that provides some basic guidance, but there are still many mysteries. For example, is it the case that multiple instances of DynamicModule in a single notebook cell will (surprisingly!) share variables? For example, if a single cell has DynamicModule[{g = 0.05}, {Append[Association[], "test" -> Dynamic[g]], Manipulator[Dynamic[g], ContinuousAction -> False]}] and also DynamicModule[{g = 0.05}, {Append[Association[], "test" -> Dynamic[g]] Manipulator[ Dynamic[g], ContinuousAction -> False]}] (note the missing comma), moving the latter slider affects the fomer!? $\endgroup$
    – Alan
    Commented Jul 14, 2023 at 20:47
  • 1
    $\begingroup$ Note how the second slider is inside the association (since the value of the association is effectively being "multiplied" by the slider) - this means that the sliders Dynamic[g], as well as the Dynamic[g] occurrences of the associations of both DynamicModule expressions are inside an association (after the expressions have been evaluated at least): As a result, the localization fails as outlined above, and the g that is being manipulated/displayed is not the one from a DynamicModule, but rather the global variable g (you can check this by evaluating g in a separate cell) $\endgroup$
    – Lukas Lang
    Commented Jul 14, 2023 at 21:06

Does this what you want?

Manipulate[{Append[List[], g], 
  Append[Association[], "test" -> g]}, {{g, 0.05}, 0, 0.1}, 
 ContinuousAction -> False

enter image description here


Note, "DynamicModule" is comparable to "Module". And "Manipulator" can be used inside "Module" or "DynamicModule" E.g. with "DynamicModule" you get:

DynamicModule[{g = 0.05}, {Append[List[], Dynamic[g]], 
   Append[Association[], "test" -> Dynamic[g]]} Manipulator[
   Dynamic[g], ContinuousAction -> False]]

enter image description here

  • $\begingroup$ Thanks but I'm trying to use DynamicModule in another setting and I need to understand its behavior. I suppose the difference in behavior is some kind of clue ... $\endgroup$
    – Alan
    Commented Jul 14, 2023 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.