MathGroup Archive 2010

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

Search the Archive

Re: how to create a mathematica package

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

On 7/21/10 at 7:14 AM, nma at 12000.org (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:

>BeginPackage["foo`"]
>f::usage = "f[x]"
>Begin["`Private`"]

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

>End[]
>EndPackage[]

>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:

BeginPackage["foo`"]

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

f::usage = "f[x]"
Begin["`Private`"]

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

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

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