Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
x = 5;
temp`x = 6;
Begin["temp`"]
{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 at 15:09
    
A duplicate was asked, might provide some extra bits of information. –  István Zachar Mar 10 at 9:27

2 Answers 2

up vote 9 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

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;
Begin["temp`"];
{x, Global`x}
End[];

{6, x}

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

{5, 5}

share|improve this answer
1  
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 at 16:09
    
(This is the same thing that you are saying, put in a different way.) –  Szabolcs Mar 4 at 16:13
1  
BeginPackage ... EndPackage puts temp` at the beginning of the $ContextPath but Begin ... End does not change the $ContextPath. –  Chris Degnen Mar 4 at 16:49

Your Answer

 
discard

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.