MathGroup Archive 2010

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

Search the Archive

Re: how to create a mathematica package

  • To: mathgroup at
  • Subject: [mg111159] Re: how to create a mathematica package
  • From: Bill Rowe <readnews at>
  • Date: Thu, 22 Jul 2010 05:43:02 -0400 (EDT)

On 7/21/10 at 7:14 AM, nma at (Nasser M. Abbasi) wrote:

>On 7/20/2010 12:43 AM, Istv=E1n Zachar wrote:
>>I think you should also protect the public variables of the package
>>at the end to prevent the user to overwrite them.

>When you said the above, I made some experiments, and I think what I
>need to do in addition to what you said is to also Clear the
>function nam= e.

>So, now I have it like this:

>f::usage = "f[x]"

>f[x_] := Module[{}, x^2];


>The Unprotect[f] is needed for the case when the package is reloaded
>again, else one will get an error trying to define something already
>protected (from the first loading)

I've implemented the following scheme in packages I've written
for myself:


Unprotect @@ Names["foo`*"];
ClearAll @@ Names["foo`*"];

f::usage = "f[x]"

f[x_] := Module[{}, x^2];

Protect @@ Names["foo`*"];

This approach saves the need to clear and protect each function
individually. It also makes it possible to load the package when
it has already been loaded using Get without generating errors.
This last is handy when you want to make changes to the package
and test those changes. Using Needs won't work for this purpose
since Needs will not load a package that is already loaded.

  • Prev by Date: Re: A Question About Directive
  • Next by Date: Re: Easy simplification with Mathematica?
  • Previous by thread: Re: how to create a mathematica package
  • Next by thread: Re: how to create a mathematica package