Re: Delay in $Context changes taking effect?
- To: mathgroup at smc.vnet.net
- Subject: [mg30039] Re: [mg30006] Delay in $Context changes taking effect?
- From: Omega Consulting <omega_consulting at yahoo.com>
- Date: Wed, 25 Jul 2001 01:00:22 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
This is a parsing problem. Before evaluating, each expression is parsed. Any new symbols in the expression are created *before* evaluation, i.e. before BeginPackage alters $Context. Ths simplest solution is to split the cell. That way "f=1" will be parsed after "BeginPackage["Test`"]" is evaluated. At 11:49 PM 7/20/2001, Michael wrote: >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