Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
x = 5;
temp`x = 6;
{x, Global`x, temp`x}

The result in my Mathematica session is {5,5,6}, but it's {6,5,6} in "Power Programming with Mathematica". (Page 231)

share|improve this question
Hi and welcome to Mma.SE. Please take time to learn how to format postings. There is a help button in the upper right toolbar of the edit box. – Michael E2 Mar 4 '14 at 15:09
A duplicate was asked, might provide some extra bits of information. – István Zachar Mar 10 '14 at 9:27
@Kuba Please re-read the documentation for $ContextPath, because this list of contexts should be searched after the symbol cannot be found in $Context. Since Begin resets the current context, the behaviour is surprising and in the book, the results are different. – halirutan Apr 7 '15 at 22:42
@halirutan will do, thanks. Haven't thought that through. – Kuba Apr 7 '15 at 22:55
I think it would be useful to move the answers below to the original question. This can be done with a Merge. If anyone disagrees please let me know soon. – Mr.Wizard Apr 8 '15 at 1:45
up vote 17 down vote accepted

This behaviour has changed since that book was published. I am writing this additional answer to make it clear how Mathematica 9 searches contexts for symbols and that even the current version 9 documentation is incorrect in describing this.

How symbol lookup actually works

When you enter a symbol name such as x, Mathematica will check if a symbol with this name already exists. It will first search the contexts from $ContextPath for x, one by one. If it doesn't find it there, it'll search the context from $Context for it. If it still doesn't find it, then it will create a new symbol named x in $Context.

Thus $ContextPath controls where to look for symbols, while $Context controls where to create new symbols.

Your observations are explained by these rules, noting that Begin will change $Context only but not $ContextPath. Note that BeginPackage will change both $Context and $ContextPath.

Warning: the documentation contains an error

The documentation states that

$ContextPath is a global variable that gives a list of contexts, after $Context, to search in trying to find a symbol that has been entered.

In fact $ContextPath is searched before $Context in the current version.

In old versions this was not the case, as the Wagner book describes. I don't know when the change happened.

share|improve this answer
Was this reported? – Kuba Apr 8 '15 at 6:03
@Kuba Is everybody sure that this is really just one more documentation bug? Maybe it is not. I at least find the documented behaviour and Wagner's description more intuitive. Someone (@Daniel Lichtblau ?) from WRI should react. – Rolf Mertig Apr 8 '15 at 8:19
@RolfMertig : Danny Lichtblau and I are looking into this. – Stefan R Apr 8 '15 at 19:37
@Stefan Anything to report at this time? – Mr.Wizard Jul 14 '15 at 2:07
@Mr.Wizard It looks like the behavior was intentionally changed in V6, but the documentation was not properly updated. It has been reported internally but not fixed so far. – Stefan R Jul 14 '15 at 19:39

It appears since version 3 shadowed variables are given priority, as demonstrated below. In the temp` context x is taken as temp`x unless Global`x exists.

Remove[temp`x, Global`x]
temp`x = 6;
{x, Global`x}

{6, x}

Remove[temp`x, Global`x]
temp`x = 6;
x = 5;
{x, Global`x}

{5, 5}

share|improve this answer
Isn't what happens that contexts from $ContextPath are searched for names before $Context? (While it is still true that new symbols are created in $Context.) Then the documentation of $ContextPath is simply incorrect because it says: "$ContextPath is a global variable that gives a list of contexts, after $Context, to search in trying to find a symbol that has been entered." – Szabolcs Mar 4 '14 at 16:09
(This is the same thing that you are saying, put in a different way.) – Szabolcs Mar 4 '14 at 16:13
BeginPackage ... EndPackage puts temp` at the beginning of the $ContextPath but Begin ... End does not change the $ContextPath. – Chris Degnen Mar 4 '14 at 16:49

It seems that when the book appeared, the behaviour of how a symbol is resolved was different. We have two important things:

  • the current $Context which is usually Global` unless you change it with e.g. Begin as you did
  • the $ContextPath which is a list of contexts that are searched when you type in a symbol like x without explicit context

Now there seems to be confusion how a symbol is found. The documentation to $ContextPath reads

$ContextPath is a global variable that gives a list of contexts, after $Context, to search in trying to find a symbol that has been entered.

This suggests that it is first checked whether a symbol is in the current $Context and after that all entries in $ContextPath are checked.

This is how it seemed to work when the book was written and it is completely understandable that you are confused why it behaves differently. In this tutorial it is written the other way around

Since $Context is searched after $ContextPath, you can think of it as having "." appended to the file search path.

Therefore, let me explain your code as I understand what happens.


You create two symbols in different contexts, the first one in Global` and the last one in temp`. As I understand it, the x in the last line is the global x because the context Global` is in $ContextPath rather than because your current $Context is Global`


With the Begin you set $Context to temp`, but that doesn't matter, because when you refer to x, it is found first in the Global` context which is an element of your $ContextPath. The rest should be clear, because when you specify the context of a symbol explicitly, there is no room for guessing.

Finally, please note that you can refer to the symbol x in the current context (meaning in $Context) by using `x and therefore


will give 6.


I haven't seen that almost the same question was already asked and has an answer which goes along what I wrote. Another important aspect was mentioned there that I haven't said: $Context controls where a new symbol is created (if there couldn't be found an existing one). Please read the following:

Result about Context is inconsistent with the description of “Power Programming with Mathematica”

share|improve this answer
I believe $Context controls where symbols are created, but it doesn't control where existing symbols are searched for. That's controlled by $ContextPath. – Szabolcs Apr 8 '15 at 0:26
@Szabolcs If this were true, how is temp`x=1 found when I call x inside Begin["temp`"]? Import[""][""] – halirutan Apr 8 '15 at 0:31
@Szabolcs Nevertheless, really embarrassing that I typed the whole answer without searching whether it already existed.. – halirutan Apr 8 '15 at 0:34
I was sloppy in my comment, see my answer for the full story. If you enter a symbol name without a context, Mma first looks for it in the contexts from $ContextPath. If it's not there, then it also looks in $Context. If it's not there, it creates it in $Context. The documentation is incorrect in that it it claims that $Context is searched before $ContextPath while it's searched only afterwards. In older versions it was the reverse (at least in v2.2 according to Nasser). – Szabolcs Apr 8 '15 at 0:34

(this is wiki answer, as just noticed it is duplicate, but will keep it here for easy reference)

The result is different from Version 2.2 and version 10.1, this is just to show the difference. Something changed between 1993 and today:

enter image description here

share|improve this answer
Ahh I see the duplication, perhaps it's best to merge the answers? The complete descriptions given here are really insightful. Usually I can see a potential duplication when typing the question, but I just missed the referenced one? – tarhawk Apr 8 '15 at 11:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.