3

I'm reading Mathematica Programming by Leonid Shifrin. And it said

ClearAll serves to clear all definitions (including attributes) for a given symbol (or symbols), and not to clear definitions of all global symbols in the system (it is a common mistake to mix these two things).

So what is the difference between these two things, which are a given symbol and a global symbol?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Sung
  • 35
  • 4
  • 5
    The "all" is the important part here: if you e.g. define f[x_] := x^2 and g[x_] := x^3, and then evaluate ClearAll[f], then f's definitions are cleared, but g's definitions are left untouched. – J. M.'s missing motivation Jul 25 '22 at 14:09

2 Answers2

5

Leonid, I think, was cautioning against using ClearAll[] alone in hopes that this would clear all definitions and provide a "clean slate". The closest equivalent to that might be ClearAll["Global`*"], although that still only clears user-generated definitions and not those in other contexts. Or you could use Quit[] or the menu entries to quit the currently running kernel and start afresh, for a more thorough cleanup that will forget all transient definitions, including packages loaded, modifications to other contexts, et cetera.

In other words: ClearAll is a "more powerful" version of Clear, but it still works only for the specific symbol(s) provided in its input. It does not, perhaps confusingly, "clear all symbols" in the session. Instead, it "clears all properties" of a given symbol.


It may be worth mentioning, for completeness, that Clear clear values of symbols, but it does not clear attributes, messages, or defaults associated with them; ClearAll clears those as well. You may also be interested in the discussion in Good clearing practices on this forum.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
3

Let me rewrite the paragraph that you quote:

In ClearAll the "all" stands for "all definitions". It does not stand for "all symbols". Therefore calling ClearAll[f] means to "clear all definitions associated to the symbol f".

Two examples:

  • Suppose you first define f to be the squaring function as in f[x_]:=x^2 and you then call ClearAll[f], then f will no longer be the squaring function, so f[12] will not give 144 but f[12]. Other symbols g, h, ... that you may have defined are not affected by ClearAll[f].
  • ClearAll[] does not mean "clear all symbols", but it means "clear all definitions of no symbol", so it does nothing at all.

To clarify what Leonid Shifrin means by "given symbol": If you call ClearAll[f] then f is the "given symbol". If you call ClearAll[f,g] then f and g are the "given symbols".

By contrast, you should read "all global symbols" as simply "all symbols". The qualification "global" refers to contexts which is a slightly more advanced topic that you can ignore at first.

Note. In this answer I used "all definitions" as an abbreviation for "all definitions, all attributes, all messages" and so on.

user293787
  • 11,833
  • 10
  • 28