Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, there is. I recommend reading documentation related to Mathematica contexts. In a nutshell, all variables belong to some context (namespace), and all variables can be accessed via their fully-qualified names of the form "ContextName`varName". If you just use "varName", Mathematica will search contexts in <code>$ContextPath</code> (try evaluating the variable <code>$ContextPath</code> to see what it is), and will use the first context where it finds that variable. In addition, each notebook specifies a context (stored in the variable <code>$Context</code>) where all its variables are stored (unless fully-qualified name is used). </p> <p>By default, for all notebooks the context is "Global`". Also by default, $ContextPath for all notebooks includes the "Global`" context (as well as "System`" and some others). The net result is that variables are shared across notebooks, and this can rather quickly become annoying. However, there's an easy solution. To create a "private" context for a notebook, evaluate the following:</p> <pre><code>SetOptions[EvaluationNotebook[], CellContext -&gt; Notebook] </code></pre> <p>This notebook will be assigned a unique context (evaluate the variable <code>$Context</code> to see what it is). Also, global context will be removed from ContextPath (try evaluating <code>$ContextPath</code> before and after the <code>SetOptions[...]</code> above to see what's going on.)</p> <p>[<strong>Update</strong>: As pointed out by rcollyer on the new Mathematica stack exchange, to set this option as the default for new notebooks, do the following: open the Options Inspector (Ctrl+Shift+O), change the scope (in the dropdown on the top) from "Selection" to "Global Preferences"; on the left expand the nodes Cell Options -> Evaluation Options, and change the CellContext setting to "Notebook."]</p> <p>Now, here's how to create a shared context:</p> <pre><code>Begin["SharedContext`"]; varShared1 = "Shared string"; End[]; </code></pre> <p>Alternatively, you could've just typed </p> <pre><code>SharedContext`varShared1 = "Shared string"; </code></pre> <p>Now you can either use the fully qualified names ("SharedContext`varShared1" will work in any notebook), or you can add the context to $ContextPath:</p> <pre><code>AppendTo[$ContextPath, "SharedContext`"] </code></pre> <p>If you do this in all notebooks, varShared1 will become visible without a fully-qualified name.</p> <p>To summarize, context work a lot like many other search paths. However, there are many subtleties (for example, if a symbol has already been defined in some other context, the Begin["SharedContext`"]/End[] block might not work as you expect -- the existing context of the symbol will be used instead of SharedContext`), so I recommend a healthy dose of experimentation and perusing the docs.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload