This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
[an error occurred while processing this directive] $Context

  • $Context is a global variable that gives the current context.
  • Contexts are specified by strings of the form " name `".
  • $Context is modified by Begin, BeginPackage, End and EndPackage.
  • $Context is a rough analog for Mathematica symbols of the current working directory for files in many operating systems.
  • See the Mathematica book: Section 2.6.8.
  • See also: Context.

    Further Examples

    Every symbol (function or variable) in Mathematica is associated with a context. This allows the same name to be used for different objects, if they belong to different contexts. The sequence of commands below will demonstrate this.
    There is a notion of a current context for symbol lookup. Here is the current context when you start Mathematica.

    In[1]:=

    Out[1]=

    The following name does not exist in memory yet. When you refer to it, a new symbol is created and is assigned to the current context.

    In[2]:=

    Out[2]=

    This is the context to which the new symbol belongs.

    In[3]:=

    Out[3]=

    This is its full name.

    In[4]:=

    Global`xxxx

    Assigning a new value to $Context changes the current context. Notice that the context name is a string ending with a backquote.

    In[5]:=

    Out[5]=

    When you refer to a name that has not been used before, once more a new symbol is created and assigned to the current context.

    In[6]:=

    Out[6]=

    This is its full name.

    In[7]:=

    Temporary1`xxyy

    We change the context yet again.

    In[8]:=

    The symbol we created in the Global` context is still accessible under its short name, because Global` is in the context search path, as explained in the Further Examples for $ContextPath.

    In[9]:=

    Global`xxxx

    The symbol we created in the Temporary1` context is not accessible under its short name, because Temporary1` is not in the context search path.

    In[10]:=

    Information::notfound: Symbol xxyy not found.

    Although xxyy is no longer recognized, the symbol is still accessible under its full name.

    In[11]:=

    Temporary1`xxyy

    A reference to xxxx accesses Global`xxxx, while a reference to xxyy creates a new symbol in the current context.

    In[12]:=

    Temporary2`xxyy

    This shows all the symbols currently accessible under short names that start with xx.

    In[13]:=

    xxxx
    xxyy

    This shows all symbols (in all contexts) whose short names start with xx. Note that Temporary1`xxyy and xxyy (which currently stands for Temporary2`xxyy) are completely independent symbols, and can be given different values and rules. When you use the short name xxyy you are referring to one or the other, depending on the current context.

    In[14]:=

    xxxx
    Temporary1`xxyy
    xxyy

    We revert to the default context.

    In[15]:=

    Out[15]=

    Usually the current context is changed not by means of an assignment to $Context, but rather indirectly, with the Begin command. This is so that the old context can be restored more easily. See the Further Examples for Begin.