0

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.

Ubiquitous
  • 863
  • 2
  • 7
  • 15
  • 3
    Because $ContextPath is searched before $Context and there is already a in Global`. – Kuba Aug 25 '16 at 10:15
  • 1
    Ad Edit: The question is not the same but the answer is. "When you enter a symbol name such as x, MMA will check if a symbol already exists.[...] If not, then it will create a new symbol named x in $Context.". And you did this at the end of the first code block. – Kuba Aug 25 '16 at 12:30
  • 1
    Shortly, symbols are created just by mentioning them, no values are needed. – Kuba Aug 25 '16 at 12:31

0 Answers0