If we evaluate these lines one-by-one, x
will be created in the context cc
.
Begin["cc`"];
x = 1;
End[]
However, if we evaluate them together,
(Begin["cc`"];
x = 1;
End[])
then x
will be created in Global
. This is despite the following printing cc`
:
(Begin["cc`"];
Print[$Context];
End[])
What is the reason for this behaviour? My guess is that contexts only matter during the parsing phase, not evaluation.
Use case: I wanted to create a palette Button
that will define some symbols if they don't exist yet, in a "private" context to avoid conflict with globals. What is the preferred method to do this, other than putting all the definitions in a package file and loading them from the palette? (I'd like to keep the palette self-contained.)