Re: how to create a mathematica package
- To: mathgroup at smc.vnet.net
- Subject: [mg111201] Re: how to create a mathematica package
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Fri, 23 Jul 2010 07:13:08 -0400 (EDT)
On Thu, Jul 22, 2010 at 1:43 PM, Bill Rowe <readnews at sbcglobal.net> wrote: > 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. > > > Hi Bill, I've written a package some time ago called PackageManipulations which allows to "remove" or reload a loaded package with functions PackageRemove and PackageReload. It seems to complement your technique. Particularly, after a given package has been "removed", it can be loaded again using Needs. I find it very convenient for interactive package development. It can be found here: http://www.mathprogramming-intro.org/additional_resources.html together with the notebook containing examples of use. Regards, Leonid