3
$\begingroup$

Initial values specified for x, y, ... are evaluated before x, y,... are cleared.

It's from the Details in The reference to Block. And I can't understand it.

When are the local variables cleared and what's the meaning of "initial values are evaluated"?

Could anyone give an example to illustrate it? Thanks a lot!

$\endgroup$

1 Answer 1

5
$\begingroup$

You have to read this together with the second bullet point:

When you execute a block, values assigned to x, y, … are cleared. When the execution of the block is finished, the original values of these symbols are restored.

Now look at the following example:

x = 3;
Block[{x = x}, Print[x]]
(* 3 *)

If symbols were cleared before the initial values were evaluated, then the output of the code would be x.

x = 3;
Block[{x = (Clear[x]; x)}, Print[x]]
(* x *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.