I thought when we type a symbol without specifying its Context
, it will search all the Context
in $ContextPath
for existing symbol.
But in Module
/Block
, or even CompoundExpression
, all the symbols will be converted to Global
Context regardless of the content of $ContextPath
.
Unfortunately, I didn't find any information in the documentation explaining this behavior.Is it a feature? What is purpose of this design?
Here is my example:
Code Here:
AppendTo[$ContextPath, "w`"];
w`ww = 1;
Context[ww]
Similar behaviour can be observed as follows:
Quiet@Remove["A`x", "B`x", "Global`x"];
{A`x, B`x} = {"a", "b"};
$Context = "A`";
Print@x;
$Context = "B`";
Print@x;
$Context = "Global`";
gives a, b
, but
(
Quiet@Remove["A`x", "B`x", "Global`x"];
{A`x, B`x} = {"a", "b"};
$Context = "A`";
Print@x;
$Context = "B`";
Print@x;
$Context = "Global`";
)
gives Removed[x]
twice.
$ContextPath
affects parsing, not evaluation. Because of the parentheses,(AppendTo[$ContextPath, "w`"]; w`ww = 1; Context[ww])
is parsed in its entirety first, and evaluation begins only afterwards. Before the first part of this is evaluated,$ContextPath
isn't changed yet. $\endgroup$Context
to symbols first, which is not in reference.wolfram.com/language/tutorial/Evaluation.html. Is the parsing step executed before the whole evaluation? $\endgroup$ToExpression
to try to falsify this hypothesis and gain more confidence. This behaviour was discussed on this site multiple times, but I was too lazy to find it. I assumed Kuba's link pointed to one. $\endgroup$