Why does this local variable show up as Global?


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

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



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

test[] := Module[{}, q = 1]

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



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

enter image description here

  • $\begingroup$ 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. $\endgroup$ Dec 22 '14 at 16:44
  • $\begingroup$ 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.) $\endgroup$ Dec 22 '14 at 16:48
  • $\begingroup$ Can you showcase a shadowing problem MWE? $\endgroup$
    – Yves Klett
    Dec 22 '14 at 16:52
  • 1
    $\begingroup$ I just marked this question as a duplicate. Please review that action and let me know if you believe it is incorrect. $\endgroup$
    – Mr.Wizard
    Dec 22 '14 at 16:55
  • $\begingroup$ @Yves - added a showcase. $\endgroup$ 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.

(* Global`x *)
  • $\begingroup$ 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. $\endgroup$ 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


is. Or that here

1 /. omfg_ :> 2

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

(* {"ohwhatthehell", "omfg", ...} *)

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

  • $\begingroup$ 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. $\endgroup$ Dec 22 '14 at 16:59

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