Contexts and Block
- To: mathgroup at smc.vnet.net
- Subject: [mg108782] Contexts and Block
- From: Chris Degnen <degnen at cwgsy.net>
- Date: Wed, 31 Mar 2010 05:27:45 -0500 (EST)
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? 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[]