Group Abstract Group Abstract

Message Boards Message Boards

1
|
9.2K Views
|
3 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Global vs local variables in notebooks

Posted 9 years ago

I am using Mathematica 8, and I find the fact that variables in notebooks are global by default to be quite inconvenient and awkward. It would be better for me if every new notebook that I open operates in its own sandbox, and does not affect the other notebooks I may have open. In one notebook, I might set some variables to some constants, and then evaluate some function based on those variables. I might even change some of the function definitions and re-evaluate those.

If I do any of these things, I will alter or destroy the work in a different open notebook, since all variable names are global. I have to completely exit Mathemetica, closing all notebooks, to get back to the results I had in Notebook #1 which I destroyed in a previous session by changing a variable in Notebook #2.

Is there a way of declaring all variables in a notebook to be local instead of global? Do I have to put the entire contents of my notebook inside of a Module[ ] or Block[ ] declaration? Is this even syntactically acceptable? Do I have to place a unique Package declaration as the first statement within every new notebook that I open?

POSTED BY: Herbert Swan
3 Replies

Hello Herbert,

as I just found out you actually do can adjust this behaviour by changing global options:

Format -> Option Inspector -> Cell Options -> Evaluation Options

there you can change "CellContext" from "Global" to "Notebook". I personally prefer the default settings where all notebooks have the same global context, because this is great for testing and debugging.

Regards -- Henrik

POSTED BY: Henrik Schachner

Thanks. This helps a lot. I still think this should be the default behavior, however. Perhaps it could be a user preference, whether notebook variables should be local or global by default.

POSTED BY: Herbert Swan

Hi Herbert,

begin a new notebook with:

SetOptions[EvaluationNotebook[], CellContext -> Notebook]

This should give each new notebook its own context.

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard