MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: Second argument of BeginPackage, revisited

  • To: mathgroup at
  • Subject: [mg78396] RE: [mg78356] Re: Second argument of BeginPackage, revisited
  • From: José Luis Gómez <jose.luis.gomez at>
  • Date: Sat, 30 Jun 2007 06:07:14 -0400 (EDT)
  • References: <f5td66$36j$><f5vs13$keg$> <>

Dear Andrew

As far as I know, you are right when you say that:

BeginPackage["a`", {"b`"}]
(* code block 1 *)

(* code block 2 *)

do NOT do "exactly" the same (the second one is called "hidden import"). 
I have written several Mathematica packages taking advantage of the
difference between this two (for a very simple example please see the sample
package code in this link: )
maybe it is not what everybody expects, maybe it is not as clearly
documented as everybody would want, and sooner or later anyone forgets the
important back-quote "`" at the end, but it is the behavior of Mathematica
since version 2.0, and you can actually take advantage of the two options.
For me, a great help in understanding Mathematica's packages was this book:
(Do not worry that it is not version 6.0, the package thing is practically
the same since version 2.0):

Programming in Mathematica by Roman Maeder (can find it in Amazon)

I am sure that the more you know it, the more you love it (Mathematica).
That happened to me.



-----Mensaje original-----
De: Andrew Moylan [mailto:andrew.j.moylan at] 
Enviado el: Viernes, 29 de Junio de 2007 04:41 a.m.
Para: mathgroup at
Asunto: [mg78356] Re: Second argument of BeginPackage, revisited

On Jun 28, 6:37 pm, dh <d... at> wrote:
> Hi Andrew,
> you misunderstood the Package concept. BeginPackage["a`", {"b`"}] will
> place b in the $ContextPath so that its content can be found, but it
> does not put b in Context[], so that new variables are not created in b.
> The following may help. Start a new session and enter:
> BeginPackage["b`",{"PhysicalConstants`"}]
> Print[SpeedOfLight];
> Print[Context[]];
> Print[$ContextPath];
> EndPackage[]
> hope this helps, Daniel
> Andrew Moylan wrote:
> > Unfortunately (in my opinion), BeginPackage["a`", {"b`"}] does not
> > call Needs[] on its second argument(s).
> > Suppose b.m looks like this:
> > BeginPackage["b`", {"PhysicalConstants`"}]
> > (* some code *)
> > EndPackage[]
> > Then calling Needs["b`"] in a new kernel results in "PhysicalConstants`"
> > being on the list of contexts, so that e.g. SpeedOfLight works as
> > But calling BeginPackage["a`", {"b`"}] instead does *not* put
> > PhysicalConstants on the list of contexts during the definition of the
> > package, so that e.g. SpeedOfLight results in the creation of a new,
> > symbol called a`SpeedOfLight.
> > Finally, for maximum confusion (in my opinion), once EndPackage[] is
> > to finish loading the "a`" package, "PhysicalConstants`" *is* placed on
> > list of contexts, causing shadowing between a`SpeedOfLight and
> > PhysicalConstants`SpeedOfLight.
> > Effectively, BeginPackage appears to
> > 1. Call Needs[] on its second arguments, then
> > 2. Temporarily remove from the context path any extra contexts those
> > packages added to the context path, then finally
> > 3. Put all those temporarily removed packages back onto the context path
> > once EndPackage[] is called.
> > Can anyone help me understand why this behaviour is useful?

Daniel, you have misunderstood the question in my post.

Here is the crucial irregularity to which I refer:

BeginPackage["a`", {"b`"}]
(* code block 1 *)

(* code block 2 *)

The above two alternatives are not identical, because (strangely, in
my opinion) code in code block 1 cannot refer to symbols defined in
any packages declared in the second argument of the BeginPackage
statement for b` (in b.m); whereas code in code block 2 *can* refer to
such symbols.

Here is a different way to understand the irregularity:

Suppose this code works correctly:

(* code block 3 *)

Then, will this code generally also work correctly?

BeginPackage["whatever`", {"b`"}]
(* code block 3 *)

The answer is no, because of the way BeginPackage apparently modifies
the value of $ContextPath after calling Needs[] on its second argument.

  • Prev by Date: Re: Suggestions for Maintaining "Object" State?
  • Previous by thread: Re: Second argument of BeginPackage, revisited
  • Next by thread: System of differential-algebraic equations