Re: Contexts and Block
- To: mathgroup at smc.vnet.net
- Subject: [mg108794] Re: Contexts and Block
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Wed, 31 Mar 2010 06:51:19 -0500 (EST)
- References: <hov81p$93a$1@smc.vnet.net>
Am 31.03.2010 12:27, schrieb Chris Degnen: > I have some code which reads in rules using variable names that > already exist, e.g. a and b. I used Block to achieve the replacement > instructions, but was surprised to have to use BeginPackage and Begin > again to get the context right. Can anybody comment on what's going > on here? I think everything behaves as expected. The function definition of TestFunction is made in context "Test`Private`" so the symbols a and b used there are actually Test`Private`a and Test`Private`b. When you use the function, it runs in context Global` , so when you read the file with Get the new symbols are created as Global`a and Global`b so that the patterns in TestFunction don't match. Only if you do set the Context to Test`Private` before reading the file, the symbols a and b will be in the context Test`Private` and the patterns will match. > BeginPackage["Test`"]; > TestFunction::usage = "Testing contexts"; > Begin["`Private`"]; > TestFunction[] := Module[{}, > > a = 1; > b = 1; > > BeginPackage["Test`"]; > Begin["`Private`"]; > > Block[{a, b}, > x = Get["x.dat"]; > a2 = a /. x; > b2 = b /. x;]; > > End[]; > EndPackage[]; > > Return[{a2, b2}]]; > > End[]; > EndPackage[]; > > x = {a -> "one", b -> "two"}; > x >> x.dat; > TestFunction[] > here are some suggestions for your code: a Module with an empty variable list does nothing, so there is no reason to use it. Also the Return at the end is not necessary. Finally I don't see any good reason to use symbols in your replacement rules. If you use strings instead you will avoid any context issues. In some cases this might also be more efficient, after all a symbol is something much more complicated than just a string. The following is basically doing the same thing with much less effort: BeginPackage["Test`"]; TestFunction::usage = "Testing contexts"; Begin["`Private`"]; TestFunction[] := ({"a", "b"} /. Get["x.dat"]); End[]; EndPackage[]; x = {"a" -> "one", "b" -> "two"}; x >> x.dat; TestFunction[]