2
$\begingroup$

When set context unique to this notebook:

ClearAll @@ { $Context<>"*" } 

Clear["`*"] 

can both clear the variables I assigned. I just wonder what's the difference between them? Are they interchangeable?

$\endgroup$
  • $\begingroup$ An obvious difference is one uses ClearAll and the other uses Clear. Fixing that, can you give an example where one of these inputs gives a different result than the other? $\endgroup$ – Jason B. Jan 17 at 1:46
  • $\begingroup$ It seems to me they always take the same effect in one notebook. $\endgroup$ – consideration Jan 17 at 2:10
  • $\begingroup$ Are you asking about Clear vs ClearAll or $Context<>"*" vs "`*"? $\endgroup$ – Kuba Jan 17 at 9:51
2
$\begingroup$

Clear vs. ClearAll

Is answered here: Good clearing practices

$Context<>"*" vs. "`*":

99% of the time it is the same thing.

See tutorial/Contexts: "`name a symbol in the current context"

See also ref / ClearAll / Examples / Applications / 2nd example

So how can it be different? If it is defined and evaluated in a different context than the context of the moment of using it. Here is an example:

BeginPackage["MyContext`"]

ClearMe[] := ClearAll  @ clearSpec1;

clearSpec1 = $Context <> "*";
clearSpec2 = "`*";

EndPackage[]

Now, if you call ClearMe[] it will correctly clear MyContext`* even though the current context is probably Global`. Had clearSpec2 been used you would clear Global`.

Ok, pretty artificial example but you get the point.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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