# How does Context work? [duplicate]

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"]; www = 1; Context[ww]  Similar behaviour can be observed as follows: Quiet@Remove["Ax", "Bx", "Globalx"]; {Ax, Bx} = {"a", "b"};$Context = "A";
Print@x;
$Context = "B"; Print@x;$Context = "Global";


gives a, b, but

(
Quiet@Remove["Ax", "Bx", "Globalx"];
{Ax, Bx} = {"a", "b"};
$Context = "A"; Print@x;$Context = "B";
Print@x;
$Context = "Global"; )  gives Removed[x] twice. • Take a look here: mathematica.stackexchange.com/q/115849/5478 – Kuba May 30 '16 at 16:39 • $ContextPath affects parsing, not evaluation. Because of the parentheses, (AppendTo[$ContextPath, "w"]; www = 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. May 30 '16 at 16:45
• @Szabolcs so you mean there is a parsing step that adds Context to symbols first, which is not in reference.wolfram.com/language/tutorial/Evaluation.html. Is the parsing step executed before the whole evaluation? May 30 '16 at 16:51
• @Szabolcs would you mind telling me how you got this information? Is it from the docs or some smart investigation? May 30 '16 at 17:00
• Well, it's just a hypothesis, but it's a very reasonable one. To come up with it, there's no other investigation needed than what you already tried and thinking a bit about how Mathematica could theoretically be implemented. You can devise additional tests with 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. May 30 '16 at 17:18

From tutorial/Contexts we learn that unknown symbols are created in the $Context when they are not found in $ContextPath. Most likely the lookup for unqualified symbols happens at the parsing stage with the values that $Context,$ContextPath have at that time.
I assume that internally, an "unqualified symbol" (one with an empty Context) is not even a thing.