Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I agree with the comments above that you should perhaps have a compelling reason to do this, but here goes. Here is a code I use in such cases, which allows your symbol to be parsed in whatever context you like, at run-time:</p> <pre><code>SetAttributes[ParseTimeNameSpaceWrapper,HoldFirst]; Options[ParseTimeNameSpaceWrapper] = { LocalizingContext-&gt;"MyLocalizingContext`", DefaultImportedContexts:&gt;{"Imported1`", "Imported2`"}, ExtraImportedContexts:&gt; {} }; ParseTimeNameSpaceWrapper[code_,opts:OptionsPattern[]]:= Module[{result, context = OptionValue[LocalizingContext], defcontexts = OptionValue[DefaultImportedContexts], extraContexts = OptionValue[ExtraImportedContexts], allContexts}, allContexts = {Sequence@@defcontexts,Sequence@@extraContexts}; BeginPackage[context,If[allContexts==={},Sequence@@{},allContexts]]; result = code; EndPackage[]; result ]; </code></pre> <p>You can use options to specify some contexts where these symbols exist, that you want to import during the parse stage. You can call this from any package or notebook, and the symbol will be parsed according to whatever context you specify.</p> <p>HTH</p> <p>Edit:</p> <p>Responding to a comment (since it made the question more specific): There is no question that at run-time <code>Context[]</code> will display whatever is the current context from where the function was called (Global in that case). I meant something else: <code>Context</code> has a syntax <code>Context[symbol]</code>, to give a context of any symbol if it is on the <code>$ContextPath</code>. For example, <code>Context[getGraphs]</code> returns <code>Bulatov'showGraphs'</code>. Therefore, if you need to determine the context of some exported function automatically, you call <code>Context[function]</code>. You can use this to construct full names of other (private) functions of that package. Here is a self - contained example:</p> <pre><code>In[1]:= BeginPackage["MyTest`"]; f[x_, y_, context_: Context[f]] := Module[{f1str = "function1", f2str = "function2", f1, f2}, {f1, f2} = ToExpression[context &lt;&gt; "Private`" &lt;&gt; #] &amp; /@ {f1str, f2str}; f1[x, y]; f2[x, y];]; Begin["`Private`"]; function1[x_, y_] := Print["In function1: arguments are ", x, " , ", y]; function2[x_, y_] := Print["In function2: arguments are ", x, " , ", y]; End[] EndPackage[]; Out[6]= "MyTest`Private`" In[8]:= f[1, 2] During evaluation of In[8]:= In function1: arguments are 1 , 2 During evaluation of In[8]:= In function2: arguments are 1 , 2 </code></pre> <p>where <code>x,y</code> are just some sample arguments. Then, you never actually supply the last argument, but you can use the <code>context</code> variable inside your function, to construct long names for your other functions, as in the sample code above. Or you could just plain use Context[f] inside body of the function, and not add any arguments to it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload