WOLFRAM LANGUAGE TUTORIAL
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 the Wolfram Language, you can use the notion of "contexts" to organize the names of symbols. Contexts are particularly important in Wolfram Language packages that introduce symbols whose names must not conflict with those of any other symbols. If you write Wolfram Language 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 , where the is the backquote or grave accent character (ASCII decimal code 96), called a "context mark" in the Wolfram Language.
Here is a symbol with short name
, and context
You can use this symbol just like any other symbol.
You can for example define a value for the symbol.
The Wolfram Language treats
as completely different symbols.
It is typical to have all the symbols that relate to a particular topic in a particular context. Thus, for example, symbols that represent physical units might have a context . Such symbols might have full names like or .
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 Wolfram Language session, there is always a current context $Context. You can refer to symbols that are in this context simply by giving their short names, unless the symbol is shadowed by the symbol with the same short name on the $ContextPath. If a symbol with the given short name exists on the context path, it will be used instead of the symbol in the current context.
The default context for Wolfram Language sessions is
No symbols with the name
exist on $ContextPath
, so the short name is sufficient to refer to the symbol in the current context.
Contexts in the Wolfram Language 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 Wolfram Language 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 the Wolfram Language can be hierarchical. Thus, for example, the full name of a symbol can involve a sequence of context names, as in .
|context `name orc1`c2 … `name||a symbol in an explicitly specified context|
|`name||a symbol in the current context|
|`context`name or`c1`c2` … `name||a symbol in a specific context relative to the current context|
|name||a symbol in the current context, or found on the context search path|
Specifying symbols in various contexts.
Here is a symbol in the context
When you start a Wolfram Language session, the default current context is . Symbols that you introduce will usually be in this context. However, built‐in symbols such as Pi are in the context .
In order to let you easily access not only symbols in the context , but also in contexts such as , the Wolfram Language supports the notion of a context search path. At any point in a Wolfram Language 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 the Wolfram Language search in a sequence of contexts to find a symbol with that short name.
The context search path for symbols in the Wolfram Language is analogous to the "search path" for program files provided in operating systems. Since $Context is searched after $ContextPath, you can think of it as having "." appended to the file search path.
The default context path includes the contexts for system‐
When you type in Pi
, the Wolfram Language interprets it as the symbol with full name
Finding contexts and context search paths.
When you use contexts in the Wolfram Language, there is no reason that two symbols that are in different contexts cannot have the same short name. Thus, for example, you can have symbols with the short name both in the context and in the context .
There is, however, then the question of which symbol you actually get when you type in only the short name . 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
This adds two additional contexts to $ContextPath
. Typically, the Wolfram Language adds new contexts to the beginning of $ContextPath
Now if you type in
, you get the symbol in the context
In general, when you type in a short name for a symbol, the Wolfram Language 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 or symbols with the same short name in the current context are effectively "shadowed". To refer to these symbols, you need to use their full names.
The Wolfram Language issues a message when you introduce new symbols that "shadow" existing symbols with your current choice for $ContextPath. In addition, in the notebook front end the Wolfram Language warns you of shadowed symbols by coloring them red.
This introduces a symbol with short name
in the context
. The Wolfram Language warns you that the new symbol shadows existing symbols with short name
Now when you type in
, you get the symbol that appears first in the context path,
If you once introduce a symbol that 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 the Wolfram Language. You can do this using the function Remove[s].
|Clear[s]||clear the values of a symbol|
|Remove[s]||remove a symbol completely from the system|
Clearing and removing symbols in the Wolfram Language.
This removes the symbol
Now if you type in
, you get the symbol
When the Wolfram Language 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.
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 the Wolfram Language 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
The Wolfram Language puts
in the current context