Why is it that not the value of b gets printed in the following example but the symbolname? How can I force the printing of the actual dynamic value of the variable?
a = {1, 2, 3};
DynamicModule[{b},
Print[Dynamic[b]];
{Dynamic[a], Dynamic[b]}
,
Initialization :> (b = Length[a]; a = a + 2)
]
output:
b$107
Out[2]= {{3, 4, 5}, 3}
Edit (after reading your answers/comments):
Consider the more simple example, without Initialization
code (to get around WReach's example):
a = {1, 2, 3};
DynamicModule[{b = Length[a]},
Print[Dynamic[b]];
{Dynamic[a], Dynamic[b]}
]
output:
During evaluation of In[4]:= b$602
Out[5]= {{1, 2, 3}, 3}
Note, that this example does what I want to if I use Module
instead of DynamicModule
or leave out Dynamic
from the Print
line. My concerns are:
Why does this second example fail to print the value of b correctly? There is no Initialization, which (according to the help) contains "an expression to evaluate when the DynamicModule is first displayed". Also according to help: "When
DynamicModule
is first evaluated, initial assignments for local variables are made first, and then any setting for theInitialization
option is evaluated."The help should read: "Initialization: an expression to evaluate when the result of
DynamicModule
is first displayed", meaning that aPrint
statement onscreen does not constitute the "result" of theDynamicModule
. If this is correct, then (and only then) I understand why thePrint
statement does not mean that theDynamic
object appears correctly.