# What's the difference between these two ways of specifying dependent packages?

What's the difference between these two ways of specifying dependent packages?

Method 1:

BeginPackage["foobar", {"xxx", "yyy"}]

Method 2:

BeginPackage["foobar"]
Needs["xxx"]
Needs["yyy"]

Do they always have the same effect? Note that these packages may be used in a big collection of packages which have complex interdependency.

-

While @rcollyer has answered the "what", I feel that this question still deserves an additional answer because no less important is IMO the why.

### Public and private package import

The first form (contexts in BeginPackage) is called the public import. What this means is that, in addition to making the public symbols of those contexts available for the implementation of your package, you also make them available for the code that loads your package, be it some top-level code or another package. The two typical use cases when you may want to do this are:

• You want at least some of the functionality of the packages you use in your package to be also available to the end user
• Your package is actually an extension of some other package, much like a subclass extends its parent class in OOP.

The difference between these two scenarios is sometimes blurred.

The second form is called private import, and is recommended for most cases when you import some functions from other packages into yours. Most of the time, you want it this way, since you only use those functions in implementation of your package's functions, and otherwise the end user could not care less what they are.

### Further information

A much better and more complete description of these mechanisms can be found in the book of Roman Maeder "Programming in Mathematica", which IMO remains to this day the best reference for package mechanics in Mathematica.

-
Likely the mechanism is implemented via Begin[{$ContextPath}, ...] (or similar) and Needs is either called before it (public import), or within the body (private import). So, that when$ContextPath reverts to its original value, any package loaded privately is removed from the $ContextPath, also. – rcollyer Nov 1 '12 at 5:15 @rcollyer Yes, could be. One can surely imitate the action of BeginPackage and EndPackage by the top-level code, that's what I think is really important - we are given a great deal of control over encapsulation, which is what I think is a right design. – Leonid Shifrin Nov 1 '12 at 5:26 +1 For the important usage guideline of generally using the second form unless necessary. I like the name "private import" too. – Andrew Moylan Nov 1 '12 at 5:48 @AndrewMoylan Thanks, Andrew. The name is not mine though, it was used by Roman in his book. – Leonid Shifrin Nov 1 '12 at 6:01 I agree with you @LeonidShifrin that Maeder's books are still the best in many respects. I only wish they would be updated/expanded to cover the current MMA version. They were written in the 90's and cover versions 2.x and maybe 3.0. Yet they are still very relevant due to the smooth evolution of MMA in the following 20 years. – magma Nov 1 '12 at 10:23 A little experimentation reveals the differences. Including the packages within BeginPackage: In[2]:=$ContextPath
BeginPackage["Temp1", {"QuantumRealHarmonics"}];
$ContextPath a = 5; EndPackage[]$ContextPath
(*
Out[2]= {"Cell$$10824", "System"} Out[4]= {"Temp1", "QuantumRealHarmonics", "System"} Out[7]= {"Temp1", "QuantumRealHarmonics", "Cell$$10824", "System"}
*)

Using Needs after BeginPackage:

In[2]:= $ContextPath BeginPackage["Temp1"]; Needs["QuantumRealHarmonics"]$ContextPath
a = 5;
EndPackage[]
$ContextPath (* Out[2]= {"Cell$$10826", "System"} Out[5]= {"QuantumRealHarmonics", "Temp1", "System"} Out[8]= {"Temp1", "Cell$$10826", "System"} *) So, the ordering of the contexts is effected which effects how different symbols are resolved. Additionally, QuantumRealHarmonics does not make it into the global$ContextPath once EndPackage is run. The second form will then not pollute the global context, unlike the first.

Note, that while they do not "pollute" the global context, i.e. you cannot refer to them by their unqualified name, they are still loaded and can be referenced by their fully qualified name. For example, QuantumRealHarmonicsRealHarmonicsMatrix would work in the second case, but RealHarmonicsMatrix would not after EndPackage[] has executed.

-
For the curious: yes. QuantumRealHarmonics is a real package in my collection. I picked it at random which, incidentally, loads a package UtilitiesMatrix via the second method (but in the private section) to avoid polluting the global context. – rcollyer Nov 1 '12 at 3:55