# Localised variable shows up as Global [duplicate]

Why does this local variable show up as Global?

?q


f[] := Module[{q}, Null]

?[q]


Globalq

This causes a shadowing warning when the the code below is run subsequently. (The q variables are not supposed to be connected in any way. They're just the same variable name.)

BeginPackage["test"];
test;
q
Begin["Private"];
test[] := Module[{}, q = 1]
End[];
EndPackage[]


q::shdw: Symbol q appears in multiple contexts {test,Global}; definitions in context test may shadow or be shadowed by other definitions. >>

q

Edit

Apparently this is a normal effect of parsing input. The shadowing can be avoided by removing q, e.g.:-

• I edited your localization into scoping. Localization, as I think it's normally understood, would mean adjustments of e.g. date and time formats for a particular (geopolitical) locale. The computer science concept is scoping, although the text of your question obviously makes it clear to which you are referring. Dec 22 '14 at 16:44
• Also, worth noting that Module here produces a symbol in the Global context in any case; it is just not called q but rather Symbol["q$"<>ToString[$ModuleNumber]]. If you want to create a symbol with a context, I think the best way is probably to use Unique. (Specify Temporary attribute to fully replicate Module behavior.) Dec 22 '14 at 16:48
• Can you showcase a shadowing problem MWE? Dec 22 '14 at 16:52
• I just marked this question as a duplicate. Please review that action and let me know if you believe it is incorrect. Dec 22 '14 at 16:55
• @Yves - added a showcase. Dec 22 '14 at 17:23

The q in Information[q], although different from the q localized in Module is indeed a Global variable. In fact, try any variable you like and you will get the same result, provided that the Context of your notebook is Global.

Information[x]
(* Globalx *)

• Thanks for the observation. I was using ? earlier, but tried Information for clarity. Didn't realise it would work differently. I'm still getting a shadowing problem though. Dec 22 '14 at 16:51

What you see here happens because Mathematica does see this symbol during evaluation. As far as I know, Mathematica has, as many compilers/interpreters, a symbol table where it puts all symbols that it has seen during an evaluation.

If you know this, it becomes instantly obvious what the output of

Information[ohwhatthehell]


is. Or that here

1 /. omfg_ :> 2


even the symbol that is hidden in a pattern makes its way into the symbol table.

Names["Global*"]
(* {"ohwhatthehell", "omfg", ...} *)


To remove a symbol from the symbol table you can use, well, Remove

Remove[omfg]
Names["Global*"]

• Hi, I have edited my question. I think you have a point in that one of many interpreters is warning me about shadowing, except the new variable is not in Global; it is localised as shown. Is it still q or q\$123`? I wonder. Dec 22 '14 at 16:59