Message Boards Message Boards

0
|
6842 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Managing variable names in notebooks?

I'm new to Mathematica. Where do I find documentation explaining how functions and variables that I define are handled when I have multiple notebooks open? I need to know things like:

  1. How do I list names I've defined in a specific notebook? For instance, I define f[x]:=x^2 and y=x. I want an easy way to see that names f and y are defined.
  2. What is the Mathematica terminology that distinguishes between names I've used (like x above) verses names that have assigned meaning (like f and y above)?
  3. How do I get Mathematica to remind me what the definition of f in this notebook, as opposed to telling me what f is in all open notebooks?
  4. Is there a way to partition a notebook so that names don't interfere with each other?
POSTED BY: Jay Gourley
4 Replies

I do a lot of practice with two or three notebooks open. So I simply clear all Global Variables at the beginning of each document, ClearAll["Global`*"]. Useful for stuff only I'm going to see.

POSTED BY: John Baxter
Posted 3 years ago

Hi Jay,

When I share a notebook with someone else I add an initialization cell with

CurrentValue[$FrontEndSession, CellContext] = Notebook

so any symbol definitions are scoped to a unique context specific to the notebook. That way I don't pollute their Global context with my stuff and vice-versa.

I prefer working with notebook specific contexts all the time. The setting can be make permanent by evaluating

CurrentValue[$FrontEnd, CellContext] = Notebook
POSTED BY: Rohit Namjoshi
Posted 3 years ago

You should also be aware of "contexts". In a notebook, the current context is Global`, given by $Context. If you want to get a list of symbols you have created, evaluate Names["Global*"]`.

Some theory that may be helpful: there's a concept in programming of static vs. dynamic. Static things are those you can read in the source, which in this case is the notebook. Dynamic is the things that happen at runtime. When you ask "what variables have I defined in my notebook", it's a static question. If you really mean "what variables have I defined so far", it's a dynamic question. The Names function is answering the dynamic question.

Partitioning different parts of the notebook will not be very convenient, in my estimation, but of course you can do it. Generally you can partition symbols by putting them in different contexts. You can do this by using packages (BeginPackage or Begin). But if you use the same "simple" name, like x, in different contexts, but both contexts are on the \ $ContextPath, you'll get a shadowing error: only one `x` from one context can "win" (be the one that gets used) and the other symbol(s) will be shadowed (hidden). The one that wins, that gets used, is the one whose context comes first on the \$ContextPath. The way to resolve the ambiguity is to "fully qualify" the variable name (basically like adding a last name to differentiate two people with the same first name), like Section1`x or Section2`x. Usually what I would do is just use longer names.

POSTED BY: Joel Klein
Posted 3 years ago

Jay: You will find the answer to all of your questions in the documentation.

One thing to note, is by default, all of your open notebooks share the same kernel. Therefore, the same names are defined in all open notebooks. Of course, there are ways to override this default setting, but I have found it to be useful.

As a starting point, check out the Names[] function.

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

Group Abstract Group Abstract