Re: Context
- To: mathgroup at smc.vnet.net
- Subject: [mg70980] Re: Context
- From: jljelinek at comcast.net
- Date: Fri, 3 Nov 2006 01:39:32 -0500 (EST)
- References: <eicnon$g4g$1@smc.vnet.net>
Daniel, when you type in a symbol, Mathematica first checks for its occurence through all the contexts on its $ContextPath, proceeding left to right. Assuming that you started a fresh Mathematica session, you should see {Global`, System`} after you execute $ContextPath. Now try the following program, which will print the current context and context path. t = "outside"; Print["before ", $Context, " ", $ContextPath]; Begin["aa`"]; Print["inside ", $Context, " ", $ContextPath]; taa = "inside-aa`"; Print[t, " taa= ", taa]; End[]; Print["after ", $Context, " ", $ContextPath]; You will see that while the current context changes as a result of having executed Begin, the context path does not. It means that if you introduce a new variable t in your program, Mathematica will first try to locate it in Global`, then in System` and having failed to find it in either, it will create a new symbol t. The new symbol will not be in the current context, though, but in the leftmost context in $ContextPath, which happens to be Global` in your case. If you want to place a symbol in a new context, then you have to use BeginPackage. Execute the following program: Print["before ", t, " ", taa]; Print["before ", $Context, " ", $ContextPath]; BeginPackage["bb`"]; Print["in ", $Context, " ", $ContextPath]; tbb = "inside-bb"; Print[t, " taa= ", taa, " tbb= ", tbb]; EndPackage[]; Print["after ", $Context, " ", $ContextPath]; Print["after ", t, " ", taa, " ", tbb]; Note that once the execution thread enters the new context bb`, the Global` context (and all other contexts if there were more of them) disappear from the context path and are replaced by bb`. (The System` context is always present.) When the symbol tbb appears, Mathematica starts looking for it left to right among the contexts on its current context path. Since it is not in bb` and it is not a system symbol either, Mathematica introduces it as a new symbol in the leftmost context, i.e., bb`, on the current context path. The program exits the bb` context on executing EndPackage and as you see, the new context bb` is now prepended to the context path. You can check where the three symbols t, taa and tbb are located by executing Context[t], Context[taa], Context[tbb]. Even though tbb exists only in the bb` context, you can access it simply by typing tbb, since it is exported by having its context placed on the context path. This mechanism is crucial for the Mathematica packages. Jan dh wrote: > Hello, > > consider: > > fun[x_]:=( Begin[x]; > > Print[t]; > > End[]; > > ); > > according to the manual one would think that the variable t in context x > > is printed. However, this is wrong! What is printed is Global`t. > > Therefore, the context to which a symbol belongs is determined during > > parsing and NOT execution. > > Can anybode give more insight and strict rules for this quirck? > > Daniel