Re: Writing functions in packages that define values of global variables
- To: mathgroup at smc.vnet.net
- Subject: [mg113751] Re: Writing functions in packages that define values of global variables
- From: Raffy <adraffy at gmail.com>
- Date: Wed, 10 Nov 2010 06:29:45 -0500 (EST)
- References: <ibb266$54v$1@smc.vnet.net>
On Nov 9, 12:53 am, "Blackwell, Keith" <Keith.Blackw... at rbccm.com>
wrote:
> Is there a way to have functions inside packages define global
> variables?
>
> Specifically, is there a way to have a function that imports a data set
> and then cuts it up and defines the pieces as variables that I can
> access with other functions I've written in a notebook.
>
> For instance, if I always want it to take the dates from the data and
> define them as variable called Dates or whatever.
You can directly define through Global`dates = {...}
You can context switch with Begin/End (I would not recommend this.)
You can expose YourPackage`dates on the context path (I'm unsure how
you define your package, typically, people put their package code
deeper in the package namespace, ie. YourPackage`Private`blah, and
then only expose what they need.)
BeginPackage["a`"];
b::usage="I'm exposed!"; (* I exist at a`b *)
Begin["`Private`"];
b = "Hello";
c::usage="I'm hidden!"; (* I exist at a`Private`c *)
c = "World";
End[];
EndPackage[];
Needs["a`"];
MemberQ[$ContextPath, "a`"] === True
??b should return usage/value
??c should return nothing (since MemberQ[$ContextPath, "a`Private`"]
=== False)