This is a highly simplified version of a initialization problem I have with a complicated program with many (about 100) modules.
If one enters: X=42; Clear[X]; the X value is removed. Fine.
Now consider 3 simple modules:
ClearMyX0[]:=Module[{}, Clear[X]];
ClearMyX1[]:=Module[{}, s=Symbol["X"]; Clear[s]];
ClearMyX2[]:=Module[{}, Clear["X"] ];
and try
X=42; ClearMyX0[]; X=42; ClearMyX1[]; X=42; ClearMyX2[];
(Print statements removed for brevity.) I find that ClearMyX0 works as expected. ClearMyX2 also works but ClearMyX1 does not. I have no idea why - my presumption about those two was the opposite.
So far I have not seen web documentation explaining that behavior. Please give me a reference.
s = Symbol["X"];there is not infomation aboutXins, only the value is assigned. – Kuba Jan 16 '16 at 22:32ClearisHoldAll. SoClear[s]means quite literally what it says. @Kuba's comment applies as well, of course. There is actually no renaming going on in any of these examples (I thought that could have been your problem at first glance), so I think there is nothing unexpected here. – Oleksandr R. Jan 16 '16 at 22:34Modulewithout a local variable doesn't change anything to what you are trying to do. If you want to remove a symbol whose name is stored in a variable as a string, you could use:ClearMyX3[] := Module[{s}, s = "X"; Clear[Evaluate@s]]. (note that I have localizeds, so in that case theModuleisn't useless which is what you probably want to do in the long term and it shows that this will also work...). In general I would never suggest to write code which needs to clear globals, can you explain why you think you need this? – Albert Retey Jan 18 '16 at 10:27