# Global vs local variables in notebooks

**URL:** <https://community.wolfram.com/t/global-vs-local-variables-in-notebooks/8313>\
**Category:** Questions\
**Tags:** mathematica, computational-notebooks\
**Created:** [April 8, 2016, 4:45pm UTC](https://community.wolfram.com/t/global-vs-local-variables-in-notebooks/8313 "2016-04-08T16:45:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hswan](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@hswan](https://community.wolfram.com/u/hswan)\
**Post date:** [April 8, 2016, 4:45pm UTC](https://community.wolfram.com/t/global-vs-local-variables-in-notebooks/8313/1 "2016-04-08T16:45:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [April 8, 2016, 5:13pm UTC](https://community.wolfram.com/t/global-vs-local-variables-in-notebooks/8313/2 "2016-04-08T17:13:10Z")

</div>

Hi Herbert,

begin a new notebook with:

```
SetOptions[EvaluationNotebook[], CellContext -> Notebook]

```

This should give each new notebook its own context.

Regards – Henrik

---

<div class="post-metadata">

**Author:** ![hswan](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@hswan](https://community.wolfram.com/u/hswan)\
**Post date:** [April 8, 2016, 6:07pm UTC](https://community.wolfram.com/t/global-vs-local-variables-in-notebooks/8313/3 "2016-04-08T18:07:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [April 9, 2016, 11:02am UTC](https://community.wolfram.com/t/global-vs-local-variables-in-notebooks/8313/4 "2016-04-09T11:02:57Z")

</div>

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
