Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to have a function, whether built-in or not, that when given a symbol, whether bound to a value or not, returns a string giving the context and name of the symbol.

Example: f[a] returns "Global`a"

Example: f[Developer`$MaxMachineInteger] returns "Developer`$MaxMachineInteger"

share|improve this question
up vote 9 down vote accepted

This works, though it'd be nicer to have a built-in way to do it:

SetAttributes[fullyQualifiedName, {HoldAll, Listable}];
fullyQualifiedName[a_] := Context[a] <> SymbolName[Unevaluated@a]

Some demonstrations:

In[4]:= fullyQualifiedName[a]

Out[4]= "Global`a"

In[5]:= foo = 3

Out[5]= 3

In[6]:= fullyQualifiedName[foo]

Out[6]= "Global`foo"

In[7]:= fullyQualifiedName[Plot]

Out[7]= "System`Plot"

In[9]:= fullyQualifiedName[Developer`$MaxMachineInteger]

Out[9]= "Developer`$MaxMachineInteger"
share|improve this answer
I bit of poking around reveals that (at least in v7) the system function Information also uses Context therefore I suppose this is the canonical method. – Mr.Wizard Jan 17 '13 at 13:26
@Mr.Wizard: Sadly no, Information only prints things, it doesn't return a string. I want to use this programmatically, not interactively. – Joel Klein Jan 17 '13 at 14:42
Sorry, I didn't make myself clear. What I mean is Information uses basically the same method you posted above, so I doubt a "better" way exists or the developers would have used it. I think your answer is the canonical method. – Mr.Wizard Jan 17 '13 at 15:44
You didn't have your employer information in your profile when I wrote the comment above and now it looks quite silly. Oh well. – Mr.Wizard Jan 21 '13 at 16:39
Well, it's a useful data point. – Joel Klein Jan 22 '13 at 2:23

Here is an alternative:

SetAttributes[f, HoldAll];
f[a_Symbol] :=      
   Block[{$ContextPath = {"Test`"}, $Context = "Test`"},

It is based on the way Mathematica treats short and long names depending on the current settings of $Context and $ContextPath. The context "Test`" must not exist for it to be fully bulletproof (it will produce short names for symbols in "Test`" otherwise), you can use some random string for it. The function produces the same output as yours. You can make it Listable as well, but for speed, it may be better to add a special rule for a list of symbols, so that the context is changed only once.

share|improve this answer
Yes, that's interesting. Maybe a UniqueContext[], a counterpart to Unique, could help eliminate the dependence on Test`. – Joel Klein Jan 17 '13 at 4:30
@JoelKlein Having UniqueContext[] would be quite handy, and not only for this problem. It would have to be efficient, however (e.g. Contexts[] can be quite slow). – Leonid Shifrin Jan 18 '13 at 8:00
@JoelKlein A UniqueContext[] would certainly help here. I take it it hasn't been implemented, though. – Emilio Pisanty Apr 15 at 19:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.