As many others (I guess) I'm struggling with the handling of contexts within Mathematica. I'm working on a new package definition with the usual syntaxt specified by the Mathematica documentation:
BeginPackage["example`"];
<Entry Points>
Begin["`Private`"]
<Definitions>
End[];
EndPackage[];
Now I want to use symbols generated in another Notebook and saved with DumpSave for efficiency. The code in this other Notebook is thus
DumpSave[<some mx file>,{symbol1,symbol2,...,symboln}]
when I import these symbols into my package it looks like this:
BeginPackage["example`"];
<Entry Points>
Begin["`Private`"]
DumpGet[<some mx file>];
<Definitions>
End[];
EndPackage[];
there is already the catch: The Symbols are not automatically loaded into the private context of the package (which is examplePrivate
in the above code snippet) but into the original context of the Notebook where the mx file has been generated (which is Global` in my case)! Thus I need to convert the symbols either into the private context in the package definition (which would the following statements
example`Private`symbol1=Global`symbol1;
example`Private`symbol2=Global`symbol2;
.
.
.
example`Private`symboln=Global`symboln;
Remove[Global`symbol1,Global`symbol2,...,Global`symboln])
or save the symbols already into the right context by using
example`Private`symbol1=symbol1;
example`Private`symbol2=symbol2;
.
.
.
example`Private`symboln=symboln;
DumpSave[<some mx file>,{example`Private`symbol1, example`Private`symbol2,
..., example`Private`symboln}]
in the generating Notebook (which would be my preferred solution). I need to do all of this clumsy setup because I do not want to have the symbols in the Global` context because then the symbols are visible outside of the package, which is not necessary and may cause shadowing problems too. Now my question: Is there a quick way to copy symbols over from one context into another? Something like
Set[Evaluate[example`Private`#],Global`#]&/@{symbol1,symbol2,...,symboln}
would be preferrable. However above statement does not work...
Any ideas?
example`Private`symbol1 = Global`symbol1;
-- why not simply change the Context toexample`Private`
before you define those symbols in the first place? $\endgroup$