# Local variables in Module leak into the Global context

From the Mathematica documentation on Module:

Module allows you to set up local variables with names that are local to the module.

But after using Module, the symbol is created !

I understand that Module makes temporary symbols appending a number, like i$8071 and those are gone afterwards. However the symbol i is newly created, but has no value or attributes. Here is a very simple session showing this. Names["Global*"] {} Module[{i}, Table[i^2, {i, 0, 10}]] {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100} Names["Global*"] {"i"} Attributes[i] {}  This is not causing me any specific trouble currently, but I want to ensure that the code I'm developing is "clean" in that it doesn't pollute the Global namespace with new symbols. Could these unwanted symbols cause trouble by shadowing symbols from other packages for example? Should I just not worry about it? How can I check that my code is "clean"? - ## migrated from stackoverflow.comJul 13 '12 at 22:54 This question came from our site for professional and enthusiast programmers. You don't even need to specify the i in the Module variable declaration, the i from the Table specification will leak out also. – image_doctor Jul 14 '12 at 1:25 I'm new to stackexchange and was unaware of how to do this. Thanks for your answers. – owler Jul 21 '12 at 15:43 @owler thank you and I'm glad you're happy with the answer. – Mr.Wizard Jul 21 '12 at 22:59 ## 1 Answer Symbols are created in the current context during parsing. This should not be a problem in normal circumstances as the symbols are merely "initialized" without values or properties. See these posts for more information: You raise good questions in the comments and I need to address them. ## Shadowing Indeed you can have problems with shadowing due to this. I intended the linked questions above to serve as examples of (and possible solutions to) this problem, but here is a short description. In a new session, if you do: Table[x, {x, 3}]; sessionx = 5; AppendTo[$ContextPath, "session"];

Information["x"]


Globalx

You can see that even though x was used in a localizing construct (Table) the Symbol Globalx was created. This then causes a shadowing problem if we also define sessionx and hope to access it with merely x by including "session" in the $ContextPath. There are some alternatives to the bare form above that prevent the creation of Globalx. ### Precedence of initialization If you know you are going to use sessionx you can initialize it first, and then if the context name is in $ContextPath the appearance of x in Table will be assumed to be sessionx (Globalx will not be created):

sessionx;
AppendTo[$ContextPath, "session"]; Table[x, {x, 3}]; Information["x"]  sessionx ### Formal Symbols You could use Formal Symbols in constructs like Table, Block, and With and Module. These symbols exist specifically to prevent conflicts of various kinds as they bear the attribute Protected and cannot accidentally have values assigned to them. (These are entered with e.g. Esc$xEsc and look much better in the Notebook than they do here.)

Table[\[FormalX], {\[FormalX], 3}];
sessionx = 5;