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.

In Section 8.1.1 (P230 of the book, P249 of the PDF) of Wagner's book, the following example is shown:

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

The output is claimed to be:

{6, 5}

But actually in version 8 and 9, the real behavior is:

enter image description here

Have I missed something, or this example is just outdated?

If so, do there exist other outdated parts in this book? Should we start a collection or something?

share|improve this question

marked as duplicate by Szabolcs, m_goldberg, bobthechemist, rm -rf Mar 8 at 19:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
may be you found a bug in the book or a bug in Mathematica :) –  Nasser Mar 8 at 9:06
1  
Try this for more strange result: x = 5; Begin["temp`"]; x = 6; {x, Global`x} which gives {6,6} ? –  Nasser Mar 8 at 9:17
    
@Nasser - If you use BeginPackage to put temp` on the context path it works, but this demo also needs a line break: x = 5; BeginPackage["temp`"]; (*line break*) x = 6; {x, Global`x} –  Chris Degnen Mar 8 at 12:21
    
The very same question was asked a couple of days ago. At that time I noted that even the current v9 documentation is incorrect because it states that when looking for a symbol that was entered, Mathematica searches $ContextPath after searching $Context, even though in reality it searches $ContextPath before $Context –  Szabolcs Mar 8 at 13:48
1  
I wrote another answer there: mathematica.stackexchange.com/a/43629/12 –  Szabolcs Mar 8 at 14:03

2 Answers 2

up vote 11 down vote accepted

This is because of $ContextPath. If the symbol outside the Begin/End is created in a context that is listed in $ContextPath, Mathematica will recognize it as belonging to that context even when called from a different context (even if Begin/End is put in a package file and called via Get):

$ContextPath
{"DocumentationSearch`", "ResourceLocator`", "PacletManager`",
 "QuantityUnits`", "WebServices`", "System`", "Global`"}
WebServices`x;
Begin["temp`"];
Print@Context@x;
End[];
 WebServices`

Now if I remove "WebServices`" from $ContextPath:

$ContextPath = DeleteCases[$ContextPath, "WebServices`"];
WebServices`x;
Begin["temp`"];
Print@Context@x;
End[];
temp`

Now what has changed in related code in successive versions, I have no idea.

share|improve this answer

Not an answer. Just screen shot. I booted up version 2.2 to verify the book result. And it does verify.

Mathematica graphics

Mathematica graphics

Something changed since the book was written !

share|improve this answer

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