Re: Contexts and Block
- To: mathgroup at smc.vnet.net
- Subject: [mg108834] Re: Contexts and Block
- From: Chris Degnen <degnen at cwgsy.net>
- Date: Fri, 2 Apr 2010 05:21:37 -0500 (EST)
- References: <hov81p$93a$1@smc.vnet.net> <hovd0m$ce6$1@smc.vnet.net>
On 31 Mar, 12:51, Albert Retey <a... at gmx-topmail.de> wrote: > ... 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[] Thanks Albert. Actually, I had to add in a reset (as shown) for the ContextPath since its rearrangement by EndPackage[] was causing some odd problems in my application. It works fine with the reset, but I will probably switch to using strings as you suggested. BeginPackage["Test`"]; TestFunction::usage = "Testing contexts"; Begin["`Private`"]; TestFunction[] := Module[{contextpath}, a = 1; b = 1; contextpath = $ContextPath; BeginPackage["Test`"]; Begin["`Private`"]; Block[{a, b}, x = Get["x.dat"]; a2 = a /. x; b2 = b /. x; ]; End[]; EndPackage[]; $ContextPath = contextpath; {a2, b2}]; End[]; EndPackage[]; x = {a -> "one", b -> "two"}; x >> x.dat; TestFunction[]