• BeginPackage["context`"] 使得 context` 和System`是仅有的活动上下文。
• BeginPackage["context`", "`", "`",
• BeginPackage 通常用在包的开头。
• BeginPackage 重新设置 $Context 和 $ContextPath的值。
• 符号名的解释与上下文有关。这样BeginPackage 影响输入表达式的分析。
• 参见 Mathematica 全书:
BeginPackage sets things up so that only System` and the named context are in the context search path. As a result, any reference to something other than a predefined symbol creates a new symbol in the named context.
The corresponding EndPackage restores the earlier environment, and also adds to the $ContextPath the context that's being concluded. As a result, symbols created between the invocations of BeginPackage and EndPackage are now accessible under their short names.
A package often needs to use variables and functions that should not be accessible by their short names ("private" symbols). Such symbols are traditionally created inside a subcontext of the context created by BeginPackage. Here is a full example illustrating the standard sequence of context control commands.
This makes Collatz` the current context, and puts only System` on the context search path.
This introduces the objects intended to be "exported", that is, made visible after the package is loaded.
This passes to a subcontext. The backquote at the beginning of `Private` stands for the current context.
Here is the definition of the function.
This tells Mathematica to revert to the previous context (here, Collatz`).
This ends the package, prepending Collatz` to the context search path.
Here is the context search path.
The Collatz function was created in the context Collatz`.
The symbol n was put in the private context Collatz`Private`. It is not accessible by its short name, so it won't interfere with other uses for n.