Bug introduced in V6 and fixed in V11.3
The behavior indeed changed but now the documentation is clear about it.
This code is inconsistent with the description from Power Programming with Mathematica:
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 Section 8.1.1 of Power Programming with Mathematica (page 231 of the hardcopy or page 249 of the PDF).
Answer
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 in Mathematica versions older than 11.3.
The $ContextPath
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.
The Contexts tutorial does correctly state the order of search in the current version:
Since
$Context
is searched after$ContextPath
, you can think of it as having "." appended to the file search path.
Comments
Post a Comment