47
votes
How to avoid nested With[]?
Introduced in V10.4 or earlier, but after V10.1
This functionality has snuck into With (ref: Daniel's comment).
Note the use of the braces.
...
35
votes
Accepted
What is the purpose of Internal`LocalizedBlock?
Internal`LocalizedBlock behaves the same as Block, but it can localize non-Symbols (e.g. f[1]...
33
votes
Accepted
Is the renaming mechanism of `With` flawed?
Short answer
The local variables of the form varname$... are used by the system, and it is unwise to use symbols with such names as local variables.
...
27
votes
Should I always be using Module in functions unless I explicitly want global variables?
The short answer is "yes", we should always use Module to localize any intermediate variables within our functions.
We might get away with using global variables ...
25
votes
Accepted
What causes Module variables to leak?
Preamble
I will try to summarize some cases I've seen or encountered, in a few rules, which I believe do hold and explain most or all of the cases I am aware of.
The rules
Here are the rules (...
24
votes
SetOptions locally?
Version 12.2
In version 12.2, use the new function WithCleanup[]:
...
Community wiki
23
votes
Accepted
Strange behavior with Cases and Position with SetDelayed
What's happening
This is not simple by any means. You have encountered another instance of a general situation with lexical scope leaks / emulation / over-protection by symbol renaming. The case at ...
23
votes
Accepted
Do Table iteration variables need to be localized using Module?
In general, it is good practice to include i among the local variables. Table does not localize its variable (or, as some say, ...
23
votes
Bug with iterated With: Referring to function arguments invalidates previous definitions
A long comment:
Automatic renaming again:
ClearAll[F];
F[x_] := Hold[With[{a = x}, {b = x}, a]];
F[0]
...

Kuba♦
- 133k
21
votes
Accepted
StringReplace, ReplaceAll and Rule interact in a bizarre way
Preamble
What happens can be understood when we recall that Rule is a scoping construct.
The general issues related to variable renamings in scoping constructs ...
21
votes
Accepted
Syntax highlighter shows error for Manipulate when it's inside a Block or a Module
Based on Mr.Wizard's answer and comments by Szabolcs and celtschk, I now understand that the code I posted does have undesirable side-effects and it should be avoided. Specifically, the scoping ...
21
votes
Accepted
I define a variable as local to a module BUT then the module uses its global value! Why?
What happens and why
As Daniel Lichtblau pointed out in comments, this behavior can also be viewed as a flaw in the current behavior / design / implementation of lexical scoping in Mathematica. ...
20
votes
Accepted
Why does the global variable exist here, where it was declared only in Module?
Why is this happening
The explanation was basically given by ciao in comments. You can also find a lot of information on this in this great answer of Mr.Wizard. I will perhaps try to view it from a ...
19
votes
Bug with Block & Lookup: Scope Variable Leak
Looks like a bug to me as well. Just as Michael I would suspect that the problem is more with Lookup than Block or ...
17
votes
Accepted
Context of localised (dynamic) symbols
I will make no attempt to defend the fact that Mathematica simulates scoping by means of variable renaming. However, the behaviour that we see is consistent with the principles under which ...
17
votes
SetOptions locally?
I have used several methods, including the ones in the other answers. I have found that the simplest method is Internal`InheritedBlock as it allows for temporary ...
16
votes
Accepted
Working with DynamicModule: SetDelayed + OwnValues
J. Fultz' answer indicates that it is not a bug but a feature which needs better documentation.
Get a habit of defining your procedures with [], e.g. ...

Kuba♦
- 133k
16
votes
Accepted
Parallelization inside a module: Proper use of local variables
There are multiple issues here. Let us take them one by one.
Why do you get 0 as the output?
There is a quirk in how ...
15
votes
What does empty {} mean in Module?
Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (...
15
votes
Accepted
Internal DynamicModule steals scope of external DynamicModule
This answer builds on @AlbertRetey's answer where, in the comments, you wanted some guidance as to how Wolfram creates robust controls.
Think very carefully about the division of labor between the FE ...
15
votes
Accepted
Why can't I inject expressions in Compile using (only) With
Compile is considered a scoping construct by the outer With, and its bindings are protected. This will work:
...
15
votes
Accepted
Rule rewriting behavior
Overview (tl;dr)
Ultimately, the difference here is due to Module being lexically scoped, while Block is dynamically scoped.
...
14
votes
Accepted
14
votes
Accepted
A With expression local variable specification without head List
This is a documentation bug. Our source notebooks do contain documentation for experimental and future features, but they are supposed to be stripped in the process of being bundled with the product. ...
13
votes
Accepted
Where does a package have to be loaded?
tl;dr
You need to call Needs before GetBoundaryMesh definition so it can be parsed (found in correct context) correctly or you ...

Kuba♦
- 133k
13
votes
RecurrenceTable iteration variable not localized? Bug or user error?
As Bob Hanlon's answer points out, RecurrenceTable does not hold its arguments, but most especially, it does not hold its iterator arguments. This must surely be ...
13
votes
Accepted
What is the accessibility of a package's `Private` context variables?
So how does MyFunction know the value of abc at the delayed function call (when it is called) if the ...
12
votes
What is the purpose of Internal`LocalizedBlock?
Disclaimer: these are all guesses, believe at your own risk.
As the first argument, you specify a list of patterns to localize. For example, {x}, ...
12
votes
Bug: With[] pattern-variable renaming misses symbols within `Except`
I agree that this is a bug. However, I want to point out that this usage of Except does not seem to be allowed in older versions.
In version 9.0:
We don't get the ...
12
votes
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
scoping × 522function-construction × 63
dynamic × 49
manipulate × 47
functions × 44
evaluation × 43
variable-definitions × 35
programming × 33
plotting × 24
packages × 20
replacement × 18
core-language × 18
compile × 17
bugs × 16
differential-equations × 15
assignment × 15
contexts × 15
performance-tuning × 13
pattern-matching × 13
variable × 13
pure-function × 13
table × 11
memory × 11
hold × 11
equation-solving × 10