Localised variable shows up as Global [duplicate]

This question already has an answer here:

Local variables in Module leak into the Global context

1 answer

Why does this local variable show up as Global?

?q

Information::notfound: Symbol q not found. >>

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

?[q]

Global`q

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

=================

=================

2 Answers
2

=================

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]
(* Global`x *)

  

 

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.
– Chris Degnen
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.
– Chris Degnen
Dec 22 ’14 at 16:59