[Date Index] [Thread Index] [Author Index]
Delay in $Context changes taking effect?
I am trying to use the $Pre and $Post functions to alter the context for variables, so that each notebook has its own set of local variables. However my attempts have not succeeded. I think that $Context is supposed to be the default context new variables are created in, and $ContextPath the paths to search for existing variables in. After a lot of research, it appears that the problem is that changes to $Context and $ContextPath do not take effect until after the current line of input in a notebook is processed. To illustrate, if I put a BeginPackage on a separate line from the assignment, the newly created variable is created in the expected context. In:= Remove[f, Test`f] In:= BeginPackage["Test`"]; f = 1; EndPackage; In:= Context[f] Out= "Test`" However, if the BeginPackage is on the same line, or in a CompoundExpression,Block,Module,If, etc. then the newly created variable is created in the wrong context: In:= Remove[f, Test`f] In:= BeginPackage["Test`"]; f = 1; EndPackage; In:= Context[f] Out= "Global`" I say the "wrong" context because there is nothing documented (that I can find) which suggests that changing $Context or $ContextPath will result in any delay until the change takes effect. For what I am doing, it is essential that the context change immediately, otherwise all my effort is for nothing. Can anybody tell me what I might be doing wrong, or perhaps an alternate method to accomplish my goal? Thanks, Michael