Re: names, symbols, and contexts
- To: mathgroup at smc.vnet.net
- Subject: [mg6221] Re: [mg6175] names, symbols, and contexts
- From: David Withoff <withoff>
- Date: Fri, 28 Feb 1997 03:21:39 -0500
- Sender: owner-wri-mathgroup at wolfram.com
> Here's the problem: a symbol fname contains the name of a Mathematica
> object, e.g.:
>
> fname = "myf"
>
> The question is how to determine the context of the object named by
> fname (in our case, the context of myf).
>
> The question comes down to how to torture fname in order to supply the
> result as argument to Context.
>
> I've tried all sorts of combinations of SymbolName, ToExpression, and
> ToString, but Mathematica keeps complaining -- error message is
> Context::ssle : Symbol, string, or HoldPattern[symbol] expected at
> position 1 in Context[...].
>
> There's something I'm missing.
>
> (Like many such things, this one is trivial to do in APL or J; I'm
> just not "getting it" how to do the same thing in Mathematica.)
>
> --
> Murray Eisenberg Internet: murray at math.umass.edu
> Mathematics & Statistics Dept. Voice: 413-545-2859 (W)
> University of Massachusetts 413-549-1020 (H)
> Amherst, MA 01003 Fax: 413-545-1801
Try Context[Evaluate[fname]].
For example, suppose there is a symbol somecontext`myf for which
the context is somecontext`:
In[1]:= Context[somecontext`myf]
Out[1]= somecontext`
and suppose there is a second symbol fname, the value of which is
a string containing the short name (the name without the context)
of somecontext`myf:
In[2]:= fname = "myf"
Out[2]= myf
Since the context of "myf" is not on the context search path (the value
of $Context, followed by the contexts listed in $ContexPath), the symbol
somecontext`myf will not be found based only on the short name:
In[3]:= Context[Evaluate[fname]]
Context::notfound: Symbol myf not found.
Out[3]= Context[myf]
If the context somecontext` of somecontext`myf is added to the context
search path, then the short name myf will be found.
In[4]:= AppendTo[$ContextPath, "somecontext`"]
Out[4]= {Global`, System`, somecontext`}
In[5]:= Context[Evaluate[fname]]
Out[5]= somecontext`
You need to use Evaluate here because the Context function has
the attribute HoldFirst:
In[6]:= Attributes[Context]
Out[6]= {HoldFirst, Protected}
Dave Withoff
Wolfram Research