# Tag Info

85

You will find a lot of information in this answer. I will add a few personal notes. Module Use Module when you want to localize variables inside your functions body, and those variables will potentially acquire and/or change their values during the computation. Basic use For example: f[x_]:=Module[{y=x^2},y=y+x;{x,y}] Here, a local mutable variable ...

32

Safety Module is safer than Block because: It is a lexical scoping construct, which means that variable bindings are only tied to a specific piece of code. Variables outside that piece of code are never affected by these bindings. In contrast, Block basically binds a variable to a piece of execution stack, not a piece of code. Such bindings are much ...

30

InternalInheritedBlock (IIB) is similar to Block, except that it preserves the original definition of the function being passed to it. The function can then be modified as we wish inside the IIB without affecting the external definition. Let's see how Block works first: f[x_] := x Block[{f}, Print@DownValues[f]; f[x_, y_] := x y; ...

24

I don't think one can avoid the need for nested With altogether - I find it a very common case to need declared variables use previously declared variables. Since I once wrote the function (actually macro) which automates nesting With, and generates nested With at run-time, this is a good opportunity to (re)post it as an answer to an exact question which it ...

23

I'll cover a few typical uses of Block, neither of which is possible using Module or With. Temporarily removing definitions When you do Block[ {a = x}, ... ] the original definition of a is effectively replaced by whatever new definition is given in the first argument of Block, for the duration of the evaluation of Block only. If we give no ...

22

Here is the almost obligatory timing response, it probably doesn't generalise very broadly but perhaps is indicative in some respects: (* no variables *) f1[x_] := (x^2; x^3;) f2[x_] := Module[{}, x^2; x^3;] f3[x_] := Block[{}, x^2; x^3;] f4[x_] := With[{}, x^2; x^3;] (* With variable definition *) f2[x_] := Module[{y = 0}, x^2; x^3;] f3[x_] := Block[{y = ...

21

The differences between Module, Block and With are nicely summarized by the results of the following expressions: x = "global"; f[] := x Module[{x = "local"}, {x, f[], Hold[x]}] Block[{x = "local"}, {x, f[], Hold[x]}] With[{x = "local"}, {x, f[], Hold[x]}] which returns: {"local", "global", Hold[x$123]} (* Module *) {"local", "local", ... 19 Scoping constructs, lexical scoping and variable renamings It pays off to understand a bit deeper how the scoping constructs work and what happens behind the scenes when you execute one. In addition to the documentation, this was discussed in part here, but let us present some summary. When the lexical scoping construct Sc[vars, body] executes (where Sc ... 18 For a single code statement, this is probably an overkill. If you have two or more of them, you have to group them in any case. CompoundExpression is one obvious choice, such as f[x_]:= ( Print[x]; x^2 ) Instead, you could also do f[x_]:= Module[{}, Print[x]; x^2 ] which is what I personally often prefer. Apart from some ... 17 I will leave the explanation of the difference between lexical and dynamic to those more qualified than myself (and to the documentation). One practical difference between Block and Module is this: Module[{x}, x] Block[{x}, x] (* -> x$1979 x *) That is, if you construct something inside a Block with a blocked variable and then return it, you may use ...

16

There will no doubt be plenty of answers for this one. However the short answer is: Use With for local constants that you don't have to change subsequently. Use Module for local variables that are local to that piece of code. Use Block for local variables that are local to that sequence of evaluation. This tutorial in the Mathematica documentation ...

16

With works by performing a substitution operation prior to executing its body, and likely it is only a single pass. So, inter-referencing the variables is not possible. Since With accepts the use of SetDelayed (:=), you might think that that could be used, instead. For example, With[{v1 = #, v2 := f[v1]}, g[v1, v2]]& @ p (* g[p, f[v1]] *) which ...

15

As a point of curiosity, I did a quick search in the source code used by Wolfram for various palettes, dialog boxes, and toolbars. About one in eight Dynamic constructs were accompanied by a With. The example you provide is certainly a good one. It illustrates the general principle nicely, but what it doesn't do is to illustrate how widely the general ...

14

This might work as you expect and be save even if definitions for x exist: Block[{x}, f[x_] = D[Sin[x], x];] I would strongly suggest that you get familiar with Derivative and pure functions if you work with symbolic derivatives, though. This will make your life much easier in the long term. Your example would reduce to: f = Derivative[1][Sin] and a ...

13

This is as designed (as it should be). In general, when you use Block[{s = someExpression}, body] then s is initialized with the value of someExpression, computed using the surrounding environment (usually Global` context). But then, all changes made to the properties of s, remain local to Block and are undone after the execution exits Block. In the ...

12

Briefly, Block allows you to temporarily change global definitions and functions in a way that Module does not. This is both its strength and weakness. If you use Block[{x = 5}, (* stuff *)] without realizing that something deep inside "stuff" relies on x you may break things. On the other hand you can use this power intentionally to do some interesting ...

12

You just have to wrap the Module around those variables, to make them semi-local: Module[{persistent}, persistent := persistent = Import["path\\file"]; SomeFunction[date_,column_]:= (body using persistent); getPce[date_,column_] := Module[{dates, value, b, e}, value = SomeFunction[...]; ] ] Here we ...

12

In your notebook, do the following: Separate the examples into cell groups. You can use, e.g., Section or Subsection cells to do this. Choose the menu item Evaluation->Notebook's Default Context->Unique to Each Cell Group. Re-evaluate your notebook. You'll now get the code isolation you're looking for. By using the menu item I point out, you're ...

12

If you look at the generated code (CompilePrint, for example), the procedure is as follows: All the program's constants are placed into separate registers (regardless of their location in the program, they can be in the r.h.s.of variable initialization in scoping constructs, or they can be statements in their bodies. Actually, same constants found in ...

11

You were asking why f[_?NumericQ] := 8 With[{a = f[a]}, Block[{NumericQ = True &}, a]] outputs f[a]. This is because of caching of the result of Conditions and PatternTests. Compare with this: With[{a = f[a]}, Block[{NumericQ = True &}, Update[]; a]] (* ==> 8 *) Generally, making global changes that might affect the outcome of a ...

11

I agree completely with J.M., Quiet is the answer. Implementing WithOff using Quiet is (as I'm sure you know) trivial. Here it is, just for fun: ClearAll[WithOff] SetAttributes[WithOff, HoldAll]; WithOff[msg_, expr_] := Quiet[expr, {msg}]; WithOff[Pattern::patv, rule = (f[x_Integer | {x__Integer}] :> g[x])]; rule2 = x_[x__] :> x;

11

Try this: g = 4.49*^3; m = 1.; s = 1.; ϵ = 2.; With[{g = g, m = m, s = s, ϵ = ϵ}, sAcceleration = Compile[{{sPosition, _Real, 1}}, (-g (m + s))/(sPosition.sPosition + ϵ*ϵ)^(3/2) sPosition]; sAcceleration2 = Compile[{{sPosition, _Real, 1}}, Module[{gg = g, mm = m, ss = s, ϵϵ = ϵ^2}, (-gg (mm + ss))/(sPosition.sPosition + ϵ)^(3/2) sPosition]]; ...

11

You cannot make definitions with patterns on the left-hand side in the first argument of a scoping construct (such as Module). You need do that in the body of the Module. You should also use a different symbol for the internal function parameter. norm[x_] := Module[{fun1, fun2}, fun1[p_] := p^2 + p - 1; fun2[p_] := p^3 - p^2 + p + 1; ...

10

Even without a minimual example, it is clear that you have a problem related to your use of With as the outer scoping construct. Please see the answers to this question, particularly mine. Use With for local constants that you don't have to change subsequently. Use Module for local variables that are local to that piece of code. Use Block for ...

10

With allows definition of local constants. An example (that I recently posted as an answer) would have been a good candidate for this: a regular expresion. With[{regex = RegularExpression[...]}, If[... something with regex ...]; If[... something else with regex ...] ] Other uses would be if you need to precalculate a local constant (Sin[20°] etc) ...

10

I don't think that there is any deeper reason for using Module[{},body] instead of just (body). Technically you are only adding overhead, as small as it might be. From the stylistic point of view I think it just adds complexity, increases what has to be read and -- as your question clearly indicates -- raises questions and adds uncertainty. I don't see any ...

10

Module does lexical scoping. This means that whatever is explicitly passed as a parameter is applied a replacement rule with the new temporary variable, just like you suggested with your ReplaceAll snippet. If you are looking for dynamic scoping, try Block. This means that while the evaluation of the Block is taking place, all calls to the localized symbol ...

10

Block isn't respected as a scoping construct from the outside. Variables aren't renamed when something is injected inside by other scoping constructs, or by a replacement, like they are in Module and With. This makes some sense because Block doesn't do lexical scoping anyway Your Table example can be explained by thinking about how a scoping construct can ...

10

I think, your main reported puzzle - the collision in the With - Block case can be understood by recalling that Block is a dynamic scoping construct, not lexical. The most important difference in our present context is that for dynamic scoping, its influence is felt by the code very late - only at the evaluation stage, because symbols' values are being ...

10

The two standard methods are SlotSequence, and the "injector pattern." Related question on StackOverflow: How to Block Symbols without evaluating them? SlotSequence ClearAll[myBlock] SetAttributes[myBlock, HoldAll] varList = {"a", "b", "c", "d"}; myBlock[args_] := Function[Null, Block[{##}, args], HoldAll] @@ (MapIndexed[Set, Join @@ ...

Only top voted, non community-wiki answers of a minimum length are eligible