Re: Mathematica scoping / contexts / variable localization

• To: mathgroup at smc.vnet.net
• Subject: [mg105065] Re: [mg105023] Mathematica scoping / contexts / variable localization
• From: "David Park" <djmpark at comcast.net>
• Date: Thu, 19 Nov 2009 05:26:00 -0500 (EST)
• References: <32142906.1258547432910.JavaMail.root@n11>

```I'm not certain why many people seem to have problems with localization of
symbols. I write lots of Mathematica definitions and code, I never use
CleanSlate or Remove or Clear all Global` symbols and I never get into
trouble (well, hardly ever).

There are a few simple Mathematica style principles that can avoid most such
problems.
1) Never assign values to simple symbols that you might conceivably want to
use as symbolic variables, for example in equation solving. To save
intermediate results while working out some development use names that you
would never use as theoretical symbols; names such as: step1, step2,
testmat, temporary.

2) Always use ClearAll[functionname] before a function definition because in
development one might easily change the lhs argument pattern, leaving old
definitions around. Also clear function names before solving differential
equations because afterwards you may want to define them from the solution.

3) Always have an Initialization Section and load all packages needed in the
notebook there, usually with Initialization cells.

The following is an example of the type of writing we often see in postings
to MathGroup.

a = .5;
b = 3;
f = Exp[-a x] Sin[b x];
Plot[f, {x, 0, 6}]

It works, but is an invitation to problems because one might later forget
that these symbols have values, or where exactly they were set. So clean up
the symbols and write a definition instead that localizes the arguments.

Clear[a, b]

ClearAll[f]
f[a_, b_][x_] := Exp[-a x] Sin[b x]

Plot[f[.5, 3][x], {x, 0, 6}]

Or if one doesn't want to keep specifying the parameters in function calls
because they aren't changed often, write:

ClearAll[f]
With[{a = .5, b = 3},
f[x_] := Exp[-a x] Sin[b x]]

Plot[f[x], {x, 0, 6}]

I apologize for a response that is more elementary than you were probably
looking for but I hope it will be helpful to many new users.

As for having a construction that automatically localized all symbols within
it, I think one would soon find that exceptions were desired, in which case
it is not much more convenient than the present case. Perhaps you meant to
localize all symbols that have a Global context? But then suppose you are
not working in the Global context? One could end up with some complicated
rules as to what is localized. It seems better in localization structures to
specify explicitly what is being localized.

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

From: Leo Alekseyev [mailto:dnquark at gmail.com]

Dear Mathematica gurus,

One of the things that initially made Mathematica difficult for me to
use was scoping -- in particular, the fact that all symbols by default
appear in the global namespace.  Even though this is a default
behavior for interactive evaluation in many packages, e.g.
R, in Mathematica, it leads to a greater potential for errors because
unlike those languages, in Mathematica
(1). a symbol can have multiple DownValues, and
(2). if one forgets to explicitly localize a symbol inside a scoping
construct, it may silently be taken from the global namespace.

After many years I finally figured out a (more or less) clean way to
structure my code and workflow, through a combination of defining
modules, contexts, packages, and careful use of Clear and Remove.

I still wonder, however, why there isn't a construct similar to Module
that would define a unique private context for _all_ symbols within
the construct (i.e. without having to declare them in a list).  You
can kind of simulate this behavior by using BeginContext["MyCont`"]
together with redefining \$ContextPath temporarily to only have
"MyCont`" and "System`".  This is obviously too verbose to be of
practical use, but I do wonder why there isn't a built-in construct.

I suppose my question is -- is there a deep wisdom behind its absence,
or perhaps I am an anomaly in thinking that such behavior (automatic
lexical scoping for symbols in subroutines, present in R and
many others) would be incredibly handy?..

Thanks,
--Leo

```

• Prev by Date: Re: Permanent Computation Efficiency
• Next by Date: Re: Define a function as a graphics directive?
• Previous by thread: Re: Mathematica scoping / contexts / variable localization
• Next by thread: Re: Re: Mathematica scoping / contexts / variable