I am trying to understand how contexts in Mathematica work. Suppose I want to write a block of code in which all newly defined symbols are in a particular context.
If I input the following code
Begin["test`"];
Context[]
a=2;
a
End[];
Context[]
a
then I get output
test`a
2
Global`
a
as I would expect. I can also type
test`a
which outputs
2
But what I don't understand is why next inputting
Begin["test`"];
Context[]
a
produces output
test`
a
The Begin["test`"]; line has shifted the context back to test`, so why does inputting a produce output a and not output 2?
Edit: I don't understand why this is a duplicate of Result about Context is inconsistent with the description of “Power Programming with Mathematica”. In that question, the symbol is assigned a value in both the local and global context. In my example I only assign a value to a in the test context.
$ContextPathis searched before$Contextand there is alreadyainGlobal`. – Kuba Aug 25 '16 at 10:15x, MMA will check if a symbol already exists.[...] If not, then it will create a new symbol namedxin$Context.". And you did this at the end of the first code block. – Kuba Aug 25 '16 at 12:30