6

After reading the documentation on the subject, I still cannot get CellContext->CellGroup to work.

Here's a simple example. (Unfortunately, I cannot easily show cell grouping, so I'll just have to describe the steps verbally as clearly as possible.)

First I evaluate this (which succeeds):

SetOptions[EvaluationNotebook[], CellContext -> CellGroup];

Then, I create two empty input cells, and group them (right-click->Group Cells).

Next, I type (but do not yet evaluate) the following two expressions into these two (now grouped) cells (one expression per cell):

foo = {};  (* first grouped cell *)
AppendTo[foo, 1];  (* second grouped cell *)

Presumably these two cells now share the same context. This will turn out to be wrong.

(FWIW, note that these cells produce no output.)

Lastly, I select the group and evaluate its contents (with shift-enter). The evaluation of the second cell fails with the following error:

AppendTo::rvalue: foo is not a variable with a value, so its value cannot be changed. >>

What am I doing wrong? Is this a bug or a feature?


Here's another, even simpler example. I proceed as before, except that this time both cells in the group contain the same expression:

$Context

(NB: no trailing semicolon!)

Now, when I evaluate the group, the output shows that the two grouped cells have different contexts:

(* Cell$$17872` *)
(* Cell$$17874` *)

One possible clue to what may be going on is the fact that when I evaluate the last pair of grouped cells, the bracket grouping the two cells disappears. What's left are 4 ungrouped brackets, two for the original input cells, alternating with their respective output cells. If I delete the output cells, the grouping bracket reappears. This may be a red herring, since, after all, in my previous example there was no output, and yet the two grouped cells behaved as if their contexts were different.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
kjo
  • 11,717
  • 1
  • 30
  • 89

0 Answers0