# For any symbol, how can I get the full context-qualified name of the symbol as a string?

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 "Globala"

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

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]= "Globala"

In[5]:= foo = 3

Out[5]= 3

In[6]:= fullyQualifiedName[foo]

Out[6]= "Globalfoo"

In[7]:= fullyQualifiedName[Plot]

Out[7]= "SystemPlot"

In[9]:= fullyQualifiedName[Developer$MaxMachineInteger] Out[9]= "Developer$MaxMachineInteger"

• 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:

ClearAll[f];
SetAttributes[f, HoldAll];
f[a_Symbol] :=
Block[{$ContextPath = {"Test"},$Context = "Test"},
ToString[Unevaluated@a]
];


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.

• 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 '16 at 19:31

This is a variation of Leonid's answer that avoids the dependence on a context like "Test" that must be empty:

SetAttributes[fq, {Listable, HoldAll}];

fq[sym_] := Block[{Internal$ContextMarks = True},ToString[Unevaluated[sym]]] fq[{fq, Print, Developer$MaxMachineInteger}]

(* {"Globalfq", "SystemPrint", "Developer\$MaxMachineInteger"} *)
`
• Very useful! Would love to see stuff like that documented instead of additional famous person curves. – Kuba Sep 29 '17 at 10:14