Wolfram Research, Inc.

2.6.8 Contexts

It is always a good idea to give variables and functions names that are as explicit as possible. Sometimes, however, such names may get inconveniently long.

In Mathematica, you can use the notion of "contexts" to organize the names of symbols. Contexts are particularly important in Mathematica packages which introduce symbols whose names must not conflict with those of any other symbols. If you write Mathematica packages, or make sophisticated use of packages that others have written, then you will need to know about contexts.

The basic idea is that the full name of any symbol is broken into two parts: a context and a short name. The full name is written as context`short, where the ` is the backquote or grave accent character (ASCII decimal code 96), called a "context mark" in Mathematica.

Here is a symbol with short name x, and context aaaa.

In[1]:= aaaa`x

Out[1]=

You can use this symbol just like any other symbol.

In[2]:= %^2 - %

Out[2]=

You can for example define a value for the symbol.

In[3]:= aaaa`x = 78

Out[3]=

Mathematica treats a`x and b`x as completely different symbols.

In[4]:= a`x == b`x

Out[4]=

It is typical to have all the symbols that relate a particular topic in a particular context. Thus, for example, symbols that represent physical units might have a context PhysicalUnits`. Such symbols might have full names like PhysicalUnits`Joule or PhysicalUnits`Mole.

Although you can always refer to a symbol by its full name, it is often convenient to use a shorter name.

At any given point in a Mathematica session, there is always a current context $Context. You can refer to symbols that are in this context simply by giving their short names.

The default context for Mathematica sessions is Global`.

In[5]:= $Context

Out[5]=

Short names are sufficient for symbols that are in the current context.

In[6]:= {x, Global`x}

Out[6]=

Contexts in Mathematica work somewhat like file directories in many operating systems. You can always specify a particular file by giving its complete name, including its directory. But at any given point, there is usually a current working directory, analogous to the current Mathematica context. Files that are in this directory can then be specified just by giving their short names.

Like directories in many operating systems, contexts in Mathematica can be hierarchical. Thus, for example, the full name of a symbol can involve a sequence of context names, as in ```name.

Specifying symbols in various contexts.

Here is a symbol in the context a`b`.

In[7]:= a`b`x

Out[7]=

When you start a Mathematica session, the default current context is Global`. Symbols that you introduce will usually be in this context. However, built-in symbols such as Pi are in the context System`.

In order to let you easily access not only symbols in the context Global`, but also in contexts such as System`, Mathematica supports the notion of a context search path. At any point in a Mathematica session, there is both a current context $Context, and also a current context search path $ContextPath. The idea of the search path is to allow you to type in the short name of a symbol, then have Mathematica search in a sequence of contexts to find a symbol with that short name.

The context search path for symbols in Mathematica is analogous to the "search path" for program files provided in operating systems such as Unix and MS-DOS.

The default context path includes the contexts for system-defined symbols.

In[8]:= $ContextPath

Out[8]=

When you type in Pi, Mathematica interprets it as the symbol with full name System`Pi.

In[9]:= Context[Pi]

Out[9]=

Finding contexts and context search paths.

When you use contexts in Mathematica, there is no reason that two symbols which are in different contexts cannot have the same short name. Thus, for example, you can have symbols with the short name Mole both in the context PhysicalUnits` and in the context BiologicalOrganisms`.

There is, however, then the question of which symbol you actually get when you type in only the short name Mole. The answer to this question is determined by which of the contexts comes first in the sequence of contexts listed in the context search path.

This introduces two symbols, both with short name Mole.

In[10]:= {PhysicalUnits`Mole, BiologicalOrganisms`Mole}

Out[10]=

This adds two additional contexts to $ContextPath.

In[11]:= $ContextPath =

Join[$ContextPath,

{"PhysicalUnits`", "BiologicalOrganisms`"}]

Out[11]=

Now if you type in Mole, you get the symbol in the context PhysicalUnits`.

In[12]:= Context[Mole]

Out[12]=

In general, when you type in a short name for a symbol, Mathematica assumes that you want the symbol with that name whose context appears earliest in the context search path. As a result, symbols with the same short name whose contexts appear later in the context search path are effectively "shadowed". To refer to these symbols, you need to use their full names.

Mathematica always warns you when you introduce new symbols that "shadow" existing symbols with your current choice for $ContextPath. If you use a notebook front end, Mathematica will typically let you select in such cases which symbol you want to keep.

This introduces a symbol with short name Mole in the context Global`. Mathematica warns you that the new symbol shadows existing symbols with short name Mole.

In[13]:= Global`Mole

Out[13]=

Now when you type in Mole, you get the symbol in context Global`.

In[14]:= Context[Mole]

Out[14]=

If you once introduce a symbol which shadows existing symbols, it will continue to do so until you either rearrange $ContextPath, or explicitly remove the symbol. You should realize that it is not sufficient to clear the value of the symbol; you need to actually remove the symbol completely from Mathematica. You can do this using the function Remove[s].

Clearing and removing symbols in Mathematica.

This removes the symbol Global`Mole.

In[15]:= Remove[Mole]

Now if you type in Mole, you get the symbol PhysicalUnits`Mole.

In[16]:= Context[Mole]

Out[16]=

When Mathematica prints out the name of a symbol, it has to choose whether to give the full name, or just the short name. What it does is to give whatever version of the name you would have to type in to get the particular symbol, given your current settings for $Context and $ContextPath.

The short name is printed for the first symbol, so this would give that symbol if you typed it in.

In[17]:= {PhysicalUnits`Mole, BiologicalOrganisms`Mole}

Out[17]=

If you type in a short name for which there is no symbol either in the current context, or in any context on the context search path, then Mathematica has to create a new symbol with this name. It always puts new symbols of this kind in the current context, as specified by $Context.

This introduces the new symbol with short name tree.

In[18]:= tree

Out[18]=

Mathematica puts tree in the current context Global`.

In[19]:= Context[tree]

Out[19]=